split function

This is a discussion on split function within the C++ Programming forums, part of the General Programming Boards category; can anybody help me make a split function please :P i need it do something like this: Code: CString Data[1]; ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    103

    split function

    can anybody help me make a split function please :P i need it do something like this:
    Code:
    CString Data[1];
    CString txt;
    
    txt="bla*blabla*blablabla*...etc" //who knows how many bla's :P
    
    split(Data,txt,"*"); //or something like this
    basically it should add only the bla's in the Data array..
    for example..
    Data[0]="bla";
    Data[1]="blabla";
    Data[2]="blablabla";
    etc..

    the big problem is how to add more rows to the array of data.. i really dunno how you do that in c++

    thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    It's easiest if you use the standard library:
    Code:
    #include <sstream>
    #include <string>
    #include <vector>
    
    class Split {
    public:
      typedef std::vector<std::string>::iterator iterator;
      typedef std::vector<std::string>::const_iterator const_iterator;
    public:
      Split ( const std::string& s, const char sep );
    public:
      iterator begin() { return split.begin(); }
      const_iterator begin() const { return split.begin(); }
      iterator end() { return split.end(); }
      const_iterator end() const { return split.end(); }
    private:
      std::string original;
      std::vector<std::string> split;
    };
    
    Split::Split ( const std::string& s, const char sep )
      : original ( s )
    {
      std::istringstream in ( original );
      std::string token;
    
      while ( getline ( in, token, sep ) )
        split.push_back ( token );
    }
    
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      Split split ( "bla*blabla*blablabla", '*' );
    
      for ( Split::const_iterator it = split.begin(); it != split.end(); ++it )
        cout<< *it <<endl;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    lol damn that looks a bit harder than expected thx

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    More efficient yes, easier perhaps not, especially for newbies who can be intimidated by the STL.

    If that scares you try using a counter to go through the array and then
    reset it everytime it comes to a '*'. The counter will be indicate how large the data array must be to store your word.

    However, once you become familiar with the STL and vectors or what have you this is the best way solve your problem.


  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    hmm lol too bad i first started with vb now i get used with c++ hard.. how do i "redim" an array ? like.. for example
    load array[count + 1] and same goes for controls.. i wanted to make a server using CAsyncSocket but i dont know how to load more..

    CAsyncSocket wsServer[0];
    wsServer[0] listens for connections
    and now the other connect with the clients.. but how do i load wsServer[count + 1] or something like that

    thanks

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    Quote Originally Posted by eXistenZ
    hmm lol too bad i first started with vb now i get used with c++ hard.. how do i "redim" an array ? like.. for example
    load array[count + 1] and same goes for controls.. i wanted to make a server using CAsyncSocket but i dont know how to load more..

    CAsyncSocket wsServer[0];
    wsServer[0] listens for connections
    and now the other connect with the clients.. but how do i load wsServer[count + 1] or something like that

    thanks
    super quick note...

    do not and i repeat do not use expressions like: [count + 1]
    very uncool

    instead use: [count++]
    looks soo much better and mo professional

    just my '2 cents'
    cheers
    Alex

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by macman
    super quick note...

    do not and i repeat do not use expressions like: [count + 1]
    very uncool

    instead use: [count++]
    looks soo much better and mo professional

    just my '2 cents'
    cheers
    Alex
    count + 1 is not the same as count++
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    instead of redimensioning an array, use the STL vector. it's the hip thing to do, and appearantly hip is more important than error free code.

    seriously, just try using vectors...it's really not that hard to learn and use
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Under the hood, STL containers will automatically increase size as necessary to hold more data when they are full (they do not all automatically shrink in size when data is removed, though). STL containers implement a change in size using dynamic memory to enlarge on demand. You can do the same thing on your own if you wish. You can either use C++ memory management syntax and use the key words new, new[], delete, and delete[] or you can use C memory management syntax with malloc() (or equivalent) and free() functions. If you want to learn how to use STL containers, which spare you the need to add memory as needed manually, or learn how to use dynamic memory yourself, consult your favorite text book, tutorial, or post pertinent questions here. Using STL containers when you can allows you to use standard code that is tried and true, though like any prewrapped software, it may not be exactly what you want, so knowing how to roll your own gives you even more flexibility. Using STL containers when you can will make your life easier, and chances are, unless you are a very good programmer, improve your programs.
    Last edited by elad; 04-18-2005 at 08:21 AM.
    You're only born perfect.

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    oh ok thanks guys ill go with the stl vectors

  11. #11
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    When learning anything new remember start of on a small scale.
    Check you understand the very basics of the <vector>, how to insert extra elements and then perhaps go on to deleting them using terms like 'new' delete[]....blah.blah.blah.

    Once you've done that then try something a little closer to the project you have in mind. Like mentioned above, consult a good book or tutorial and make sure you know how the very basics work so that you have a sound foundation to extend your project.

    Yes the STL is intimidating at first, but isn't everything like that if you've never done it before? What makes it easy? Familiarity and practise.

    good luck.


  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    lol yea.. stl vectors dont look that hard as far as i seen but anyway this is a very small problem compared to what im trying to do.. i made a chat program in visual basic and since i cant make it cool because vb is limited i decided to switch to c++ and yea you got more control and you dont need to use crappy ocx anymore so i guess ill stay with c++ but its hard.. if you wanna try the program go here www.olympus-studios.biz/chatex you'll find screenshot and info there :P ill keep my server on the main one ( 24/7 ) seems to be offline for some reason
    so anyway it will take me a while to recode that into c++ AND add alot more cool stuff lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. Split function
    By fkheng in forum C Programming
    Replies: 7
    Last Post: 07-08-2003, 09:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21