Vectors in Functions

I'm trying to create a function that will take in string s and
vector<string> words. In my main function, I'm trying to change the 1st and 2nd elements of the vector words using the function, but I can't seem to figure out how to do it. I use cout << words[0] << "\n"; to test if the vectors has anything, but it doesn't.

Help would be great!


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
  
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

void line_to_vector(string s, vector<string> words)
{
    string k = s;
    string a = "";
    for (char x : k)
    {
        if (x != ' ')
        {
            a += x;
        }
    }

    string b = a.substr(0,9);
    string c = a.substr(9);

    words[0] = b;
    words[1] = c;
}


int main()
{
  string s = "num_lineslogfile.txt";
  vector<string> words = {"",""};
  
  line_to_vector(s,words);

  cout << words[0] << "\n";
  cout << words[1] << "\n";

}
Pass the vector by reference.
 
void line_to_vector(string s, vector<string>& words)
Thanks.
Topic archived. No new replies allowed.