how to copy between vector <string>

hello anyone,
btw this is a part of my program

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
void query::load_query(const char* filename){
  string lines;
  int count = 0;


  ifstream file (filename);


  //READ OPERATION--ONE EXECUTION ONLY
  if(file.is_open())
  {

    while(! file.eof() )
    {
    getline (file,lines);

    //MAKE ALL STRING LOWERCASE
    transform(lines.begin(), lines.end(), lines.begin(), (int(*)(int)) tolower);

    //CONVERT STRING TO CHAR*
    char * clines = new char [lines.length()+1];
    strcpy (clines, lines.c_str());

    //COUNTING + COPYING OPERATION
    cut_string x(clines);
    vector<string> flds = x.split();



   for (int k = 0; k < flds.size(); k++) {

        cout<<flds[i]<<endl;
        count++;
       };
    }

    file.close();
  }
   total = count;
};


the 'flds' on the code above has vector <string> data type, i was able to output it using cout but i don't know how to copy its value to another vector <string>...whenever i tried to do that using my own way, the compiled program ended up crashing...do you know how to solve this? thx before :)
#include <iterator>
.....

vector<string> flds = x.split();
vector<string> v;
v.reserve( flds.size() );

copy( flds.begin(), flds.end(), back_inserter( v ) );
Last edited on
In line 32, cout<<flds[i]<<endl;, i is not defined. Either i is defined outside the function or the code should not compile.
Last edited on
You need to compile and see what is happening every now and then, because I don't think your data structures will work the way you think they are.

You are also making some elementary mistakes with file handling -- which is understandable, because file handling is no where near as simple as people think it is.

1
2
3
4
5
6
7
8
9
10
11
12
13
  ifstream file(filename);

  if (file)
  {
    string line;
    while (getline(file, line))
    {
      transform(line.begin(), line.end(), (int(*)(int))tolower);

      // split each line into fields here...
      // I'm not sure how you are doing this, so please tell me more.
    }
  }

As noted above, I'm not sure what the goals of this assignment are. How is each line split into fields? On whitespace? Or by some other criteria?

Are you trying to get the total number of fields in the file? Or average per line? Or what?

Hope this helps.
@sharma
i'm sorry i was wrong, it should be k, not i :)

@duoas
actually i use alternative version of strtok() (i found the code here, its name is splitstring.cpp), and i use it to split only the words (without comma, space, etc) on every line that is being read...the vector <string> flds here can only temporarily store one line for each process, so i have to copy the contents of it to another vector <string>, and the main problem here is i am unable to copy it because the size of flds depends on the line in the text file that is being read, and i want to store the whole splitted text into my new vector <string>....i have tried to use resize() for the new vector <string> but it still keep making the program crash...

i also don't know how to copy vector from certain elements..

hope this can explain anything, i'm sorry for my bad english..
Last edited on
looks like i have found my own solution for this problem :)

1
2
3
4
5
6
7
8
9
10
11
 
.......

for (int k = 0; k < flds.size(); k++) {

        temp.resize(count, flds[k]);
        count++;
       };
    }
......
Topic archived. No new replies allowed.