vector or map names: Can this be a varbiable?

Hello and thanks in advance for the reply,

I have a text file which is in the following format, it is called "words data.txt":

word,word,word,word,...

word,word,word,word,...

...and so on for 53 lines. Every line is simply words separated by commas.

I need each line to become a vector using a loop function so that I can access it in later code.

Therefore, In my code i need for each line:
1st word of the line (of the text file) = name of vector
each word (including the first word) = values of the vector

Here is the snipet of code pertaining to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    ifstream chronosphere2("words data.txt");
    int vectorsize;
    vectorsize = pCWN.size();
    int countn = 0;

    //Vector that will have all the stored words

    vector< vector <string> > chrothes;
    vector <string> gename; // vector to be placed in chrothes
    while (countn < vectorsize)
    {

        string line;
        getline(chronosphere2,line );
        istringstream fullline(line);


        if (!line.empty())
        {
            string line2;
            while (line2 != "")
            {
             getline(fullline, line2, ',');
             gename.push_back(line2);

            };
        };
        chrothes.push_back(gename);
        gename.clear();

        ++countn;
    };
    if (chrothes.empty())
    {
        cout << "This is empty.";
    }
    else
    {
        cout << chrothes.size();
        string blah;
        blah = chrothes[0][0];
        cout << blah << endl;

    };


Everytime I compile and run, this crashes.

I wanted to use a map struct, but it's the same problem as vectors:

I can't use a variable for the name, or am I wrong?

Can this somehow be done:

1
2
3
4
5
6
7
string nameofvector; //name of vector obtained from txt file as a string
map <string, vector <> > parentmap; 
.....getline().....
Lets say, first word = word1
Lets say, words in the line = word1,word2,word3...in a vector called nameofvector

parentmap[word1] = nameofvector //note that nameofvector is a variable that changes with each line of code in the file 


Any suggestions? I hope I was clear.
The reason for this is because of line 20 and 21, therefore line 41 is a segmentation fault. To fix it, on line 21, set the string variable to any other string that is not ""

EDIT:
Also since you already know the number of lines in your text file, you can initialise your vector chrothes to contain that many vectors:

vector< vectorsize, vector <string> > chrothes;

Then line 28 can be:

chrothes[countn++] = gename;
Last edited on
Thanks smac89!

Listen, just a quick question that has been troubling me:

Is there a way to feed vectors (or map) a variable as a name that changes with each loop?

vector <string> variable; //where variable changes with each vector made?

IF not, is there any way of creating many vectors with different names according to a list of names I have?

Thanks in advance!
Not the way you are thinking; if there was, how would you refer to them later in your program since the name in the file could be anything? You could make a map that associates strings with vectors though, which would basically accomplish the same thing.

std::map<std::string, std::vector<std::string>>
tacitus wrote:
is there any way of creating many vectors with different names according to a list of names I have?


Not that I know of, you can do what @Zhuge has shown which will be to use a mapping to map string names to a vector object.

Note that if you plan to store a lot of these objects, then you might want to use unordered_map (hash table) rather than map (binary tree)
You could make a map that associates strings with vectors though, which would basically accomplish the same thing.


I declare the string into the map.
ie.
map <string, vector <string>> wordmap;

where string = variable.

Can I then use variable:

wordmap[variable]

Is that possible?
I tried to add the suggestions, but something is still wrong. File crashes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//Opening file again
    ifstream chronosphere2("words data.txt");

    map<string, vector <string> > wordsyn; //create a map that associates strings with vectors

    int counter = 0; //Counting for the pCWN

    while (!chronosphere2.eof(), counter < pCWN.size())
    {
        vector <string> gename; // vector to be placed in wordsyn which is a map
        string line;
        getline(chronosphere2,line ); //get char array
        istringstream fullline(line); //convert to string


        if (!line.empty()) //check if line is empty
        {
            string line2 = "nothing";
            while (line2 != "\n")
            {
             getline(fullline, line2, ',');
             gename.push_back(line2);

            };
        };
        wordsyn[pCWN[counter]] = gename;
        gename.clear();
        ++counter;
    };
    string returning;
    cout << wordsyn[0].size();
    //returning = wordsyn["amazed"];
    //cout << returning[0]; 
Solved. I had a neverending while loop lines 18 - 19 which was mentioned by smac89, but it flew over my head.

Thank you smac89 and Zhuge!!!!

Main conclusion from this post:

You can't introduce variables as names for structs (map/vector), but you can use maps to map names of vectors and therefore reach the same goal.

Topic archived. No new replies allowed.