2

I'm pretty new to coding, and I was hoping someone could help me out? I'm trying to read in a line of space delimited integers and parse them into (ultimately into a linked list) a vector.

so once i have a vector of ints, there are iterators for STL vector, but how can i iterate through the nodes in a link list not in STL?

#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

int main(int argc, char** argv) {    
    cout << "Enter some integers, space delimited:\n";
    string someString;
    getline(cin, someString);

    istringstream stringStream( someString );
    vector<string> parsedString;
    char splitToken = ' ';

    //read throguh the stream
    while(!stringstream.eof()){
        string subString;
        getline( stringStream, subString, splitToken);
        if(subString != ""){
        parsedString.push_back(subString);
        }
   }

    return EXIT_SUCCESS;
}
StillLearningToCode
  • 2,271
  • 4
  • 27
  • 46
  • Remind yourself of this later: `std::istream_iterator first(stringStream), last; std::vector parsedString(first, last);` for splitting a string to a container (though I don't think that's what you were really trying to do). There's also other ways in a popular question on this site. – chris May 05 '13 at 04:17
  • why not just `int a; while (cin >> a) { // do something }` – gongzhitaao May 05 '13 at 04:17
  • you want to implement the linked list yourself? – gongzhitaao May 05 '13 at 04:32
  • "want" is not exactly the word.. but yeah, im a student and my prof is insistant on doing everything the hardest way possible.. – StillLearningToCode May 05 '13 at 05:14

2 Answers2

1

Since it's space delimiter, why not just:

#include <iostream>
using namespace std;

int main() {
    int a;
    vector<int> v;
    while (cin >> a) {
        v.push_back(a);
    }

    for (int i = 0; i < v.size(); ++i) {
        int b = v[i];
    }

    return 0;
}

And BTW, ctrl-D or a non-integer input, like char, will terminate this while.

gongzhitaao
  • 6,566
  • 3
  • 36
  • 44
1

stringstream can automatically handle delimiters like this:

cout << "Enter some integers, space delimited:\n";
string someString;
getline(cin, someString);

istringstream stringStream( someString );
vector<int> integers;
int n;
while (stringStream >> n)
    integers.push_back(n);
nullptr
  • 2,244
  • 1
  • 15
  • 22
  • wouldn't i still need to use a getline to capture the keyboard input into someString? – StillLearningToCode May 05 '13 at 04:36
  • ok, so now i have the vector, i know how to make an iterator for the STL vector, but if i write a linked list class, there isn't an STL iterator for that (and i cant write one correct?). how would you suggest i loop through the linked list? i have a pointer defined for head and current. – StillLearningToCode May 05 '13 at 05:02
  • @StillLearningToCode You should post that as a different question, and look into [std::list](http://www.cplusplus.com/reference/list/list/) – nullptr May 05 '13 at 05:04
  • @nullptr could you precise what contains (stringStream >> n) ? I mean why it is detected as false at the end of the line ? –  Apr 28 '21 at 10:14