1

I have a text file that contains these data and the data is be dynamic it can be more or less than 5 columns.

5:346:87:131:1:
4:463:57:152:8:
7:345:77:121:5:

i want to use 2D vectors so that i can get output like this from the vector:

cout<<BData[0][0]<<endl; i will get 5
cout<<BData[1][0]<<endl; i will get 4
cout<<BData[2][2]<<endl; i will get 77

how can i get my 2D vector to get this results?

1 Answers1

1
std::vector< std::vector<std::string> > BData;

// split inputs and load the container

// use your code...

cout<<BData[0][0]<<endl; // i will get 5
cout<<BData[1][0]<<endl; // i will get 4
cout<<BData[2][2]<<endl; // i will get 77

Read How to split a string in C++? for information on how to split strings in C++

Community
  • 1
  • 1
NirMH
  • 4,769
  • 3
  • 44
  • 69
  • sorry if im not clear, i want to know how to use the vector to get these results, as im pretty clear on how to split them – user2625464 May 27 '14 at 07:37
  • @user2625464 - an STD `Vector` has an interface somehow similar to an `array`, so `[]` is accessing a specific index within it. if the `vector` is well initialized, it acts as an `array` – NirMH May 27 '14 at 08:07
  • @user2625464 - if my answer helped you - kindly approve it. thanks – NirMH May 27 '14 at 11:02