Help with stringstream

I have been having difficulty using stringstream in a loop. Upon investigation, I have found that stringstream is apparently not intended to be used in loops, so I tried to get around it by writing a function and then having my other function call that function. Unfortunately, I receive a compile error when I try to do this - I've been trying to search for how to call functions in other functions but I mainly see information about this in the body as opposed to "custom" functions like I'm trying to write.

Basically, I am trying to take a vector with numeric string values and transform it into a vector with long values. (The test case I use is small, the actual values I will be using are much larger and the vectors will also have more elements. I just wanted to make sure that the logic structure worked before I used it with the full set of values.)

Here is what I have so far:

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
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

void transformVector (vector<string> &myvectorstr, vector<long> &myvectorint){
  int vectorsize = myvectorstr.size();
  for (int i = 0; i < vectorsize; i++) {
      myvectorint[i] = StringToInt(myvectorstr[i]);
  }
}

long StringToInt (string value){
  long myint;
  stringstream ss(value);
  ss >> myint;
  return myint;
}

void coutVectorInt (vector<long> myvector) {
    int sizemyvect = myvector.size();
    cout << "The integer vector elements are: ";
    for (int i = 0; i < sizemyvect; i++) {
        cout << myvector[i] << " ";
    }
}

void coutVectorStr (vector<string> myvector) {
    int sizemyvect = myvector.size();
    cout << "The string vector elements are: ";
    for (int i = 0; i < sizemyvect; i++) {
        cout << myvector[i] << " ";
    }
}

int main()
{
  vector<string> inputvector;
  vector<long> intvector;
  vector<long> sumvector;

  inputvector.push_back("123");
  inputvector.push_back("456");
  inputvector.push_back("789");

  coutVectorStr(inputvector);
  cout << endl << endl;

  transformVector(inputvector, intvector);
  coutVectorInt(intvector);
  cout << endl;

  return 0;
}
Declare the function StringToInt()
And push_back() into the vector.

1
2
3
4
5
6
7
8
9
10
11
using namespace std;

long StringToInt (string value) ; // ****

void transformVector (vector<string> &myvectorstr, vector<long> &myvectorint){
  int vectorsize = myvectorstr.size();
  for (int i = 0; i < vectorsize; i++) {
      //myvectorint[i] = StringToInt(myvectorstr[i]);
      myvectorint.push_back( StringToInt( myvectorstr[i] ) ) ; // ****
  }
}


Or, simply:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <sstream>
#include <vector>

std::vector<long> transform_vector( const std::vector<std::string>& seq )
{
    std::vector<long> result ;

    for( std::size_t i = 0 ; i < seq.size() ; ++i ) // C++98
    {
        std::istringstream stm( seq[i] ) ;
        long value ;
        if( stm >> value ) result.push_back(value) ;
    }

    return result ;
}
Thank you for the response! I was able to use the second cope snippet you provided :)

Another issue I had, aside from stringstream, is that if I called StringToInt inside transformVector then I would receive an error that StringToInt wasn't defined in that scope. Just for further clarification to me: can I call one "custom" function into another (e.g. StringToInt inside transformVector)? (I feel like it would make sense that this should be doable somehow?) If so, how do I use one function inside another?

Thank you again, greatly appreciated!
Another issue I had, aside from stringstream, is that if I called StringToInt inside transformVector then I would receive an error that StringToInt wasn't defined in that scope.

Did you notice line 3 in the first snippet provided by JLBorges?

StringToInt wasn't visible in transformVector. Adding a declaration of StringToInt before the definition of transformVector makes it visible.
Oh! I didn't realize JLBorges was declaring StringToInt first for that reason - I thought s/he was just correcting the way I was storing the information to the new (long) vector. Thank you!
Topic archived. No new replies allowed.