Thread: Print vector of vector

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    61

    Print vector of vector

    Hi everyone,

    I am trying to do a map so i can build a game.

    My ideia was to use a vector of vector.

    I want to make a function to print the map but i donīt know how, i am just able to print the map inside the construtor.

    Code:
    class Map
    {
    public:
        Map(int);
        void printf(const vector <vector<int>> & );
    
    private:    
        vector <vector <int> > m_map;  // Could this be just map ?
    };
    Code:
    map.cpp
    
    #pragma once
    #include <iostream>
    #include <vector>
    #include "Map.h"
    
    using namespace std;
    
    
    Map::Map(int size) {    
        
        if (size < 10) {
            cout << "size to small, need to be at least 10x10" << endl;
            return;
            //TODO output to console
        }
        cout << "size is ok, going to create it" << endl;
        
    
    
        vector <vector <int> > map;
        // Vector of vector
        for (int i = 0; i < size; i++) {
            vector<int> temp;
            for (int j = 0; j < size; j++) {
                temp.push_back(i);
            }
            map.push_back(temp);
        }
    
    
        // Need to make in separete code.
        for (unsigned int i = 0; i < map.size(); i++) {
            for (unsigned int j = 0; j < map.size(); j++) {
                cout << map[i][j];
            }
            cout << endl;
        }
    }
    
    //void Map::printf(const vector <vector<int>>& map){
    //    for (unsigned int i = 0; i < map.size(); i++) {
    //        for (unsigned int j = 0; j < map.size(); j++) {
    //            cout << map[i][j];
    //        }
    //        cout << endl;
    //    }
    //}
    Code:
    int main() {
        Map myMap(22);
    
        myMap.printf(myMap);
        getchar();
        return 1;
    }

    The error i got is the following.
    no suitable user-defined conversion from "Map" to "const std::vector<std::vector<int, std::allocator<int>>,
    Last edited by thinkabout; 12-02-2017 at 06:16 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,715
    Your printf method shouldn't take any parameters, all of a class's members are directly accessible from inside its methods.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    61
    Quote Originally Posted by GReaper View Post
    Your printf method shouldn't take any parameters, all of a class's members are directly accessible from inside its methods.
    Code:
    Map::Map(int size) {	
    	
    	if (size < 10) {
    		cout << "size to small, need to be at least 10x10" << endl;
    		return;
    		//TODO output to console
    	}
    	cout << "size is ok, going to create it" << endl;
    	
    
    
    	vector <vector <int> > map;
    	// Vector of vector
    	for (int i = 0; i < size; i++) {
    		vector<int> temp;
    		for (int j = 0; j < size; j++) {
    			temp.push_back(i);
    		}
    		map.push_back(temp);
    	}
    
    
    	m_map = map;
    }
    
    
    
    
    void Map::printfMyMap(){
    	for (unsigned int i = 0; i < m_map.size(); i++) {
    		for (unsigned int j = 0; j < m_map.size(); j++) {
    			cout << m_map[i][j];
    		}
    		cout << endl;
    	}
    }
    Working .

    I already have tried that but i was forgetting the m_map = map on the constructor.

    Any ideias what is should do next ? my objective is the following
    Print vector of vector-capturar-jpg

    I have a console.h that the teacher give it to me.

    I think i will turn my grid in something like 50 20 50 20 50 20, so later i can turn the numbers intro colors.

  4. #4
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    778
    Nice work! There are few minor tweaks possible, but nothing important right now.

    I already have tried that but i was forgetting the m_map = map on the constructor.
    You could also use the m_map member directly, instead of a temporary map object you later assign to it.

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    61
    Quote Originally Posted by -Adrian View Post
    Nice work! There are few minor tweaks possible, but nothing important right now.


    You could also use the m_map member directly, instead of a temporary map object you later assign to it.
    I wish the work of that picture was mine.

    I donīt have a clue how i should do the board and put colors on my numbers.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,715
    This looks like a nice assignment. If your teacher has provided you with the functions necessary to write directly to parts of the console( something like gotoxy() ), then it's just a matter of laying out the tiles. You could have different numerical values for different tile types, preferably with the use of an enum. Then it would be as simple as looping through all of the map's tiles and printing the appropriate color with the use of a switch statement. Like this:
    Code:
    // gotoxy(i, j)
    switch (tile)
    {
    case Tile::Yellow:
        // set current color to yellow
    break;
    case Tile::White:
        // set current color to white
    break;
    // More cases may go here
    }
    // print tile
    Last edited by GReaper; 12-03-2017 at 09:42 AM.
    Devoted my life to programming...

  7. #7
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    778
    Quote Originally Posted by thinkabout
    I wish the work of that picture was mine.
    No, I really do mean you writing your own code and solving problems. Many beginners here never reach that stage, all the while using terrible formatting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 09-22-2013, 07:00 AM
  2. how to print a vector?
    By matrix866 in forum C Programming
    Replies: 10
    Last Post: 10-27-2011, 07:27 AM
  3. how to print/access the 1st element of the first set of the vector
    By kapil1089thekin in forum C++ Programming
    Replies: 1
    Last Post: 08-11-2010, 02:28 PM
  4. Trying to print toString from object held in vector
    By whiskedaway in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2009, 06:13 AM
  5. Reverse Print Vector Array help
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2002, 06:47 PM

Tags for this Thread