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?
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
Quickest simple tutorial I could find: http://www.cppreference.com/cppvector/all.html
Has working code.
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