String to vector

The goal is to take a string like: "a -1 4 -1 -1 -1 -1 -1 8 -1"
and turn in into a vector that holds -1 at [0] (skips a, no letters), 4 at [1], ect..

This is proving harder then I thought it would, thanks for any help!

Can letters appear in the middle of the string? The answer to this determines whether you'll have to use 1 layer of stringstream, or 2 layers of stringstream.

Look up std::stringstream.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Example program
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    // I know this isn't a complete solution, but shows std::stringstream in action.
    std::istringstream iss("a -1 4 -1 -1 -1 -1 -1 8 -1");
    
    std::string dummy;
    iss >> dummy; // throw away the initial 'a'
    
    int num;
    while (iss >> num) // "while we're able to extract an int from the stringstream"
    {
        std::cout << "Parsed: " << num << '\n';   
    }
}
Last edited on
> Can letters appear in the middle of the string?

If yes, using the regular expressions library would be simpler. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <vector>
#include <regex>

int main() {

    const std::string str = "a -12bc 345 678de fg-9098ijk m76 -- ---54321!! nop" ;
    std::vector<int> numbers ;

    {
        // optional leading sign, one to eight decimal digits
        const std::regex number_re( "[+-]?\\d{1,8}" ) ;
        std::sregex_iterator iter( str.begin(), str.end(), number_re ), end ;
        for( ; iter != end ; ++iter ) numbers.push_back( std::stoi( iter->str() ) ) ;
    }

    for( int v : numbers ) std::cout << v << '\n' ;
}

http://coliru.stacked-crooked.com/a/175da95fbce61f57
@RslWlsn3, could you be a bit clearer as to what you are accepting or not in the way of items within your string?

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
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

vector<int> getNumbers( string str )
{
   vector<int> result;
   stringstream sstr( str );
   string word;
   while ( sstr >> word )
   {
      stringstream sword( word );
      int n;   string s;
      if ( sword >> n && !(sword >> s) ) result.push_back( n );   // int ... and nothing else
   }
   return result;
}

int main()
{
   string str = "a -12bc -5-15 +12+24 345 678de -10 +500 fg-9098ijk m76 -- 123 ---54321!! nop";
   vector<int> numbers = getNumbers( str );
   for ( int i : numbers ) cout << i << " ";
}


345 -10 500 123
Last edited on
Use istream::peek() to skip ahead until you see a digit. The only complication is dealing with +/- characters.
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
#include <iostream>
using std::cin;
using std::cout;

int
main()
{
    int ch;
    int sign {1};
    while ((ch = cin.peek()) != EOF) {
	if (isdigit(ch)) {
	    int num;
	    cin >> num;		// read the number
	    num *= sign;	// add the sign
	    cout << num << '\n'; // print it
	    sign = 1;		// reset the sign
	} else {
	    ch = cin.get();	// read one character
	    if (ch == '-') {
		sign = -sign;
	    } else if (ch == '+') {
		;
	    } else {
		sign = 1;	// reset the sign
	    }
	}
    }
}

Topic archived. No new replies allowed.