Finding the first non-white space character using sscanf (C++ equivalent)

Discussion in 'C++' started by Dilip, Oct 10, 2006.

  1. Dilip

    Dilip Guest

    I have piece of code that breaks apart a string filled with spaces into
    distinct strings each containing the apporpriate non-whitespace
    characters using sscanf

    char encodedsymbol[] = "IBM RRR XO";
    char first[20], second[20], third[20];

    sscanf(encodedsymbol, "%s%s%s", first, second, third);

    Is there a C++ way to do this?

    I had a little templated code that tokenizes a string based on a
    delimiting character like this:

    template<typename Str, typename Separator, typename Container>
    void str_tokenize(const Str& str, const Separator delim, Container&
    cont)
    {
    std::stringstream stream(str);
    Container::value_type token;
    while (!std::getline(stream, token, delim).fail())
    cont.insert(cont.end(), token);
    }

    (where Container is almost always a vector<string> type)

    I know I can modify this slightly to account for spaces... but I was
    just wondering if there was a more straightforward C++ way to do it?
     
    Dilip, Oct 10, 2006
    #1
    1. Advertisements

  2. Dilip

    Dilip Guest

    Found it myself.

    string ess(encodedsymbol);
    istringstream iss(ess);
    string first, second, third;

    iss >> first >> second >> third;
     
    Dilip, Oct 10, 2006
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.