0

How can i declare such a vector, something like this vector<vector<string>>vec(1)(2) for the usage of vec[0][0] and vec[0][1] without using dinamic sizes with push_back.

1
  • The size of an std::vector can expand at run-time, and does not work as a multidimensional array. Nesting two vectors would give you freedom to have sub-vectors of different sizes for each dimension, which is probably not what you want. Take a look at Boost.MultiArray, perhaps it will help. Commented Mar 16, 2013 at 22:35

2 Answers 2

5
using namespace std;
array< array< string, 2 >, 1 > vec;

std::vector is for dynamic size arrays.
std::array (C++11, or use Boost library) is for fixed size arrays.

Sign up to request clarification or add additional context in comments.

Comments

3
std::vector<std::vector<string>> vec(1, std::vector<string>(2));

Then you can access vec[0][0] and vec[0][1]. (You can change the size of the vector though).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.