Insert into 3D vector

How do I insert a number into a certain index of a 3D vector?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  ifstream database("file.txt");
  string line;
  vector<int> C(0, 0);
  vector< vector<int> > B(0,C);
  vector< vector< vector<int> > > A(0 ,B);
  if(database.is_open()){
    int userID, itemID, timelog, result;
    while(getline(database, line)){
      stringstream getint(line);
      getint >> userID >> itemID >> result >> timelog;
      A.insert(A.begin()+userID-1, B.insert(B.begin()+itemID-1, C.insert(C.begin()+timelog-1, result)));
    }
  }
  database.close();


After I've done this, I realize that insert does not return a value. What is the best I can do here?
You would need to create a new vector for the first 2 dimensions so that the third one could have something to be placed in. I think another possible solution to this problem you have is have just 1 vector and a structure, and make some functions to find the correct structure. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Item
{
int userID, itemID, timeLog, result;
};
std::vector<Item> itemList;
...
while (getline(database, line)) {
...
Item item;
item.userID = userID;
item.itemID = itemID;
item.timeLog = timeLog;
item.result = result;
itemList.push_back(item);
}


And by lookup functions I mean something like:

1
2
3
4
5
6
7
8
9
10
11
Item& findRecordByItemID(std::vector<Item> &list, int id)
{
    int length = list.size();
    for (int i = 0; i < length; ++i)
    {
        if (list[i].itemID == id)
        {
            return list[i];
        }
    }
}


Then after inserting this stuff into item list you could do something like:

1
2
Item item = findRecordByItemID(itemList, 24);
std::cout << item.userId << " " << item.timeLog << "\n";
Last edited on
I've done quite some research, and I think set will be better than vector. But there is one problem that I don't know. If I have the struct as you suggest, then what member will set use to sort?
And why would I prefer set more than vector, because the amount of data is a lot. Then binary search would be a better search algorithm than sequential search, right? But then again, sometimes I need to search for the itemID first to find others stuff, sometimes I need to find the userID before all. I know what I want, and I know that a sorted array is easier to search, but I couldn't think of a way to sort it properly.
Oh, I figure out myself!!

M.push_back(vector <vector<int> >());

And just resize it where it is not large enough!! Tks!!
Topic archived. No new replies allowed.