Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Software Development
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory modem monitor motherboard network printer problem ram registry router security slow software sound toshiba trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
c++ string split to array???

Reply  
Thread Tools
redivivus's Avatar
Senior Member with 1,464 posts.
 
Join Date: Mar 2006
Experience: 8th hindu reincarnation -- Praise Allah
18-Sep-2006, 04:45 PM #1
c++ string split to array???
Yo yo. Is there any way in C++ to mimic what string.split() would accomplish in javascript!!? I desire to split a large string into many elements of an array. I cannot find out if there is any solution to this. Seems like all the examples split the string manually which is fine if you dont have 1000 entries to split up lol.

Any ideas?
__________________
Knowing others is wisdom;
Knowing the self is enlightenment.
Mastering others requires force;
Mastering the self needs strength.
~ Lao Tzu

If the rest of them can survive only by destroying us, then why should we wish them to survive?
~ Ayn Rand
dquigley's Avatar
Computer Specs
Senior Member with 112 posts.
 
Join Date: Apr 2006
Location: Woodinville, WA
Experience: Advanced
18-Sep-2006, 05:39 PM #2
Yup use a dynamic "array". For some samples, Google this "string.split vector c++"

Best,
Dan
redivivus's Avatar
Senior Member with 1,464 posts.
 
Join Date: Mar 2006
Experience: 8th hindu reincarnation -- Praise Allah
18-Sep-2006, 11:39 PM #3
Okay i did some research. This is my first C++ app and im pretty lost with this stuff.

Im trying to make this work, but its not working. Can you help me?

Code:
vector<string> split(const string& value, char separator, int LENG)
{
    vector<string> result;
    for (int i=1;i<=LENG;i++)
    {
        string::size_type pos = value.find_first_of(separator);
        value = value.substr(0, pos) + "%" + value.substr(pos+1);
        result.push_back(value.substr(0, pos));
    }
    return result;
}
I had it working but then i needed to replace the separator every time so it wouldnt get the first one over and over. I think the problem is trying to edit the const string??

Also do you think there would be a more efficient way than running the loop thousands of times?
__________________
Knowing others is wisdom;
Knowing the self is enlightenment.
Mastering others requires force;
Mastering the self needs strength.
~ Lao Tzu

If the rest of them can survive only by destroying us, then why should we wish them to survive?
~ Ayn Rand
GCDude's Avatar
Senior Member with 400 posts.
 
Join Date: Apr 2005
Location: Brighton, UK
Experience: Possibly Competent
19-Sep-2006, 07:10 AM #4
you dont need to check every character, find_first_of can be given a param of where to start, so u just need an interation for each seperator. Something like this should work.

Code:
vector<string> split(const string& strValue, char separator)
{
    vector<string> vecstrResult;
    int startpos=0;
    int endpos=0;

    endpos = strValue.find_first_of(separator, startpos);
    while (endpos != -1)
    {       
        vecstrResult.push_back(strValue.substr(startpos, endpos-startpos)); // add to vector
        startpos = endpos+1; //jump past sep
        endpos = strValue.find_first_of(separator, startpos); // find next
        if(endpos==-1)
        {
            //lastone, so no 2nd param required to go to end of string
            vecstrResult.push_back(strValue.substr(startpos));
        }
    }

    return vecstrResult;
}
Note this doesnt handle the case if there is no seperator. add your own case for that.
AGCurry's Avatar
Senior Member with 431 posts.
 
Join Date: Jun 2005
Location: Kansas City area
Experience: advanced but learning
19-Sep-2006, 09:33 AM #5
Why not just use strtok() ?
Shadow2531's Avatar
Senior Member with 2,640 posts.
 
Join Date: Apr 2001
19-Sep-2006, 09:36 AM #6
Here's an example:

Code:
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>
using namespace std;

inline vector<string> split( const string& s, const string& f ) {
    vector<string> temp;
    if ( f.empty() ) {
        temp.push_back( s );
        return temp;
    }
    typedef string::const_iterator iter;
    const iter::difference_type f_size( distance( f.begin(), f.end() ) );
    iter i( s.begin() );
    for ( iter pos; ( pos = search( i , s.end(), f.begin(), f.end() ) ) != s.end(); ) {
        temp.push_back( string( i, pos ) );
        advance( pos, f_size );
        i = pos;
    }
    temp.push_back( string( i, s.end() ) );
    return temp;
}


int main() {
    const vector<string> test( split( "1,2,3,4,5,6,7,8,9", "," ) );
    copy( test.begin(), test.end(), ostream_iterator<string>(cout, "\n" ) );
}

/* g++ -Wall -Wextra this.cpp -o this -O3 -mtune=i686 -s */
It doesn't support the limit param like the JS split() does, but I assume you don't usually use that. Also, just like the JS split(), if 2 delimeters are right next to each other ( or at the beginnin/end ), a blank string is added to the array. If you don't want that, in the function, check the string to see if it's empty before pushing back().

Do note that the boost library already has exactly what you want and more.

http://www.boost.org/libs/tokenizer/char_separator.htm
http://www.boost.org/libs/tokenizer/index.html

STL's mingw distro comes with the boost library, so you can use it just like the stl headers. ( Some functions of the library require that you link with libraries that you must build. Boost regex and Boost filesystem are a few of them. There are more, but STL's distro comes with regex and filesystem already built so you can link with them. Boost tokenizer is header-only, so you just include the header and go for it. )
Shadow2531's Avatar
Senior Member with 2,640 posts.
 
Join Date: Apr 2001
19-Sep-2006, 09:54 AM #7
Quote:
Originally Posted by AGCurry
Why not just use strtok() ?
Are you saying something like this?

Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstring>
using namespace std;

int main() {
    char str[] = "1,2,3,4,5,6,7,8,9";
    char* tok = strtok (str, ",");
    vector<string> parts;
    while ( tok != NULL ) {
        parts.push_back( tok );
        tok = strtok (NULL, ",");
    }
    copy( parts.begin(), parts.end(), ostream_iterator<string>(cout, "\n") );
}
( Assuming that the target storage is an array of std::strings. )
redivivus's Avatar
Senior Member with 1,464 posts.
 
Join Date: Mar 2006
Experience: 8th hindu reincarnation -- Praise Allah
19-Sep-2006, 12:21 PM #8
Thanks for the replies. I will try this again when i get home. =)

As for the last one, what does 'tok' stand for? Might help me understand it.
Shadow2531's Avatar
Senior Member with 2,640 posts.
 
Join Date: Apr 2001
19-Sep-2006, 02:32 PM #9
Quote:
Originally Posted by redivivus
As for the last one, what does 'tok' stand for? Might help me understand it.
str is short for string
tok is short for tokenizer
strtok is short for string tokenizer
tok in the last example is just the variable name I used to store the pointer strtok returns.
redivivus's Avatar
Senior Member with 1,464 posts.
 
Join Date: Mar 2006
Experience: 8th hindu reincarnation -- Praise Allah
19-Sep-2006, 08:06 PM #10
Well... i gave it another shot. It just wasnt working though. I was trying to split the string "two three four five" up and it kept putting "8" as the first value.

I ended up subbing in your code, GCDude, and it actually works. I honestly cannot see the difference between yours and the one i had (that was returning 8) except variable names. =( I must have had some problem lol...

When i changed it to your code it also fixed the problem i encountered when i tried to run the program with Dev C++ ... it was scrolling through screens of symbols and beeping for some reason.

Thx all. =)
__________________
Knowing others is wisdom;
Knowing the self is enlightenment.
Mastering others requires force;
Mastering the self needs strength.
~ Lao Tzu

If the rest of them can survive only by destroying us, then why should we wish them to survive?
~ Ayn Rand
Reply

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 10:24 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.