Thread: Split a char vector

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Split a char vector

    I have a char vector with text in it.

    I'd like to split it into strings at every new line.

    Is it better to assign the vector to a string first or is there a function that can do this with the vector?
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Of course you can do this with the vector directly: it's a matter of finding the new line sequence, and then constructing a string from a starting index to the index of the new line sequence.

    On the other hand, it might be easier with a std::string object. Since you're going to treat the data like a string anyway, why not use a std::string instead of a vector of char to begin with?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    My vector is used as an unsigned char array, I guess I could assign a string object to an unsigned char array too.

    Thank you laserlight.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. split char array, store and return it
    By paola in forum C Programming
    Replies: 3
    Last Post: 07-30-2020, 04:55 PM
  2. Replies: 4
    Last Post: 03-21-2016, 09:38 AM
  3. Sorting STL Vector of Pointers (Split)
    By Giules in forum C++ Programming
    Replies: 6
    Last Post: 11-19-2010, 07:35 AM
  4. Howto? split and use a char array
    By djnicovski in forum C++ Programming
    Replies: 18
    Last Post: 07-23-2010, 11:18 PM
  5. Replies: 9
    Last Post: 04-04-2008, 12:41 PM

Tags for this Thread