Announcement

Collapse
No announcement yet.

C++ question: strings

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • C++ question: strings

    Hello,

    I have a programming question... Suppose I have an std:: string with the contents "1,2,3,4,5"; and I want to convert it to 5 integer values.
    One option is to use
    Code:
    char buffer[80]
    strcpy (burffer, input.c_str());
    char* token = strtok(buffer, ",");   // strtok modifies the argument
    int a = atoi(token)
     char* token = strtok(null, ",");
    int b = atoi(token)
    ...
    Is there a C++ equivalent to do this?
    I know I can convert a string to an integer using sstream:
    Code:
    std::istringstream OutputStream(myString);
    int a;
    OutputStream >> a;
    But is there a way to split an std::string into tokens (similarly to strtok) ?

    I seem to recall there is a way of specifying delimber chars for input streams, but don't remember....

    Thanks!


    Jörg
    pixar
    Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

  • #2
    Found a nice implementation:
    Explore 10 key facts about Object-Oriented Programming (OOP), covering its principles, impact on languages, design patterns, and ongoing evolution.



    Jörg
    pixar
    Dream as if you'll live forever. Live as if you'll die tomorrow. (James Dean)

    Comment

    Working...
    X