Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++ 2D vectors?


ghost's Avatar
0 0

Hello, I have started to program a little in C++ and I have this thing I need to use two-dimensional vectors to do, but I don't know how to use them. I know how they work and all that but i don't know how to use them in the code. I'd appreciate if anyonw here could write a small example code where you use two-dimensional vectors in a few ways so I can see how it's meant to be written.

Many Thanks IceCube


ghost's Avatar
0 0

Come on, this isn't any advanced C++, it's just basic things.


ghost's Avatar
0 0

Bump…


ghost's Avatar
0 0

Sorry for another bump but I want to learn how to do this.


ghost's Avatar
0 0

Not entirely sure what kind of example you wanted so I just whipped this up real quick.

///////////////////////////////////////////////
// Created By: Nirucesis
// Purpose: To show how to implement 2D vectors
///////////////////////////////////////////////

#include <iostream>
#include <vector>

int main()
{
    std::vector< std::vector<std::string> > Vector_2D;
    std::vector< std::string > FlatVector, FlatVector2;
    std::vector< std::vector<std::string> >::iterator RowIt;
    std::vector< std::string >::iterator ColIt;
    
    FlatVector.push_back("silly"), FlatVector2.push_back("man");
    FlatVector.push_back("man"), FlatVector2.push_back("silly");
    FlatVector.push_back("rabbit"), FlatVector2.push_back("rabbit");;
    
    Vector_2D.push_back(FlatVector);
    Vector_2D.push_back(FlatVector2);
    
    for(RowIt = Vector_2D.begin(); RowIt != Vector_2D.end(); ++RowIt)
    {
        for(ColIt = RowIt->begin(); ColIt != RowIt->end(); ++ColIt)
               std::cout << *ColIt << std::endl;
        std::cout << "\n";
    }
    
    std::cin.get();
    return 0;
}

-Nirucesis


ghost's Avatar
0 0

Thanks a lot both of you :) I'll look through the site and the code now, I might have a question or two about it but i don't think so.