insert element into nested vector

Hello, I am very beginner in c++ and has been doing some things. I have faced a problem where i want to add a element inside the nested vector. Ill post my code to let you see how far i went.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    vector< vector<string> >all;
    int id = -1;

    int create(){
       string fName = "";
       string lName = "";
       
       vector<string>info;
       cout<<"Enter first name"<<endl;
       cin>>fName;
       cout<<"Enter last name"<<endl;
       cin>>lName;

       id++;
       string pId = NumberToString(id);

       info.push_back(fName);
       info.push_back(lName);
       info.push_back(pId);
       cout<<"data is created"<<endl;
    
       all.push_back(info);
       return 0;
    }

As you can see here i implemented a function where user enters last name and first name every time this function runs. So i have a another function where i enter a id of a person it will give me chance to add another element inside it. Its like i want to add a element inside the first or maybe the second child of parent vector shown in the code. But somehow i cannot manage to do it. I tried to push_back a element on the first child like all[0].push_back(desc) but it doesn't works.
Can you post the rest of your code? I see what you are trying to do in the first part, but I need to see the part where it is failing before I can determine what the problem is. Also, post what error messages you are receiving.
Have you considered a vector of a class or structure that holds this information?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct Test
{
    std::string fName;
    std::string lName;
    std::string id;
};

int create(Test& test);

...

    Test test;
    create(test);
...

int create(Test& test)
{
   
       cout << "Enter first name"<<endl;
       cin >> test.fName;
       cout << "Enter last name"<<endl;
       cin >> test.lName;

...



@Wyboth ok sure here is the another part of the code where im failing
1
2
3
4
5
6
7
8
9
    int more(){
       string desc = "";
    
       for(int i=0; i<all.size(); i++)
    
       cout<<"Enter description of this person"<<endl;
       cin>>desc;
       all[0].push_back(desc);
    }

its not working i know that im doing it wrong but dont know how to properly do this. I tried many ways to put a element named description into the first child or maybe second by choosing its id.
@jlb no my advisor told me to do this without using class, he said that its possible to do this stuff with vectors and simple statements, but im really failing on it.
Last edited on
There are a couple of problems I see with the rest of the code you posted, and they don't have to do with the way you set up your vector. First, you forgot the brackets for your for loop. Second, you put all[0] instead of all, so it would push each one to the back of the zeroth element in all (assuming the problem with the for loop brackets is fixed). Here is the corrected code. See if this works for you, and if not, post your entire code, from start to finish, along with what error messages, if any, you are receiving.

1
2
3
4
5
6
7
8
9
10
11
void more()
{
    string desc = "";

    for (int i = 0; i < all.size(); i++)
    {
        std::cout << "Enter description of this person: ";
        std::cin >> desc;
        all[i].push_back(desc);
    }
}


I also agree with jib; a class/struct would be far better suited for this situation than a vector of elements. Yes, you [i]can
use a vector, but it is not the right tool for the job. I don't know why anyone would tell you not to use a struct for what it's built for, unless the point of this exercise is to show you how poorly suited a vector is for this task, and how much easier it is to use a struct, unless you actually don't know how to use a struct (in which case, it is time to learn how to use it). If your advisor actually thinks using a vector to do this is simpler than a struct, you should find a new advisor, because they are dead wrong. A struct allows you to access each element by name, whereas with a vector, you need to remember which element is stored at which index, and hope you didn't assign to the wrong one at some previous point in your code. You are only making things harder for yourself by using a hammer to drive a screw.
1
2
3
4
5
6
7
8
9
10
11
12
int more(){
    string desc = "";
    vector<string>test;
    for(int i=0; i<all.size(); i++){
        test=all[i];
    }
    cout<<"Please Enter the description"<<endl;
    cin>>desc;
    all[0].push_back(desc);

    cout<<"description = "<<test.back()<<endl;
}

But it shows the id of a person not the description i added. I expected it would be the description. I tried many other ways but i forgot the what error messages it gave me, and I already tried many things in my code so there is no errors right now
Last edited on
i suggest to change this:
exit(EXIT_SUCCESS);
with this:
return 1;
Last edited on
Topic archived. No new replies allowed.