vector of substrings

This is a discussion on vector of substrings within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> int main(int argc, char *argv[]) { std::string data("111001001110111010101110111011101110111|101011000010001010101000100000101010101|101001001110011011101110111000101110111|101001001000001000100010101000101010001|111011101110111000101110111000101110111"); std::vector<std::string> fontMap; // How to fill the above vector ...

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    vector of substrings

    Code:
    #include <iostream>
    
    int main(int argc, char *argv[])
    {
        std::string data("111001001110111010101110111011101110111|101011000010001010101000100000101010101|101001001110011011101110111000101110111|101001001000001000100010101000101010001|111011101110111000101110111000101110111");
        std::vector<std::string> fontMap;
        // How to fill the above vector with substrings from data, separated at '|' ?
        return 0;
    }
    Edit: Suggest a solution using only std stuff, please!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    strtok perhaps?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Banned
    Join Date
    Nov 2007
    Posts
    678
    dang!
    strtok is C specific, works on char* mess! *_*
    please C++ natural solution

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    dang!
    strtok is C specific, works on char* mess! *_*
    please C++ natural solution
    Well, I guess you'd have to write something like strtok() then - I personally would use strtok() on a copy of the string you get from c_str() - because it works well and doesn't cause any real problems.

    But if you are "under no circumstances allowed to use <cstring>", then you will have to wait for one of those guys that can drum up clever STL stuff that will solve this - I don't know enough STL to solve it that way.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    you are always quick with help Mats, thanks
    you are right, i need some STL specific solution, even i do not know much of it.

    other wise a simple solution would be to use strtok or something similar to get substrings, convert them to std::string and just push_back to std::vector, but that seems too tedious, i was thinking something better . . .

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    you are always quick with help Mats, thanks
    you are right, i need some STL specific solution, even i do not know much of it.

    other wise a simple solution would be to use strtok or something similar to get substrings, convert them to std::string and just push_back to std::vector, but that seems too tedious, i was thinking something better . . .
    Not too tedious [subject to bugs]:
    Code:
    char *d = new char[strlen(data.c_str())+1];
    strcpy(d, data.c_str());
    char *p = strtok(d, "|");
    while(p) {
       fontMap.push_back(string(p));
       p = strtok(NULL, "|");
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Banned
    Join Date
    Nov 2007
    Posts
    678
    thanks Mats, that seems simpler, i can wrap the code in my choice of function, which i *really* missed in std::string class, something like:
    Code:
    std::vector<std::string> split(std::string data, std::string delim) {
    	std::vector<std::string> vec;
    	char *d = new char[strlen(data.c_str())+1];
    	strcpy(d, data.c_str());
    	char *p = strtok(d, delim.c_str());
    	while(p) {
    		vec.push_back(string(p));
    		p = strtok(NULL, "|");
    	}
    	delete[] d;
    	return vec;
    }

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    thanks Mats, that seems simpler, i can wrap the code in my choice of function, which i *really* missed in std::string class, something like:
    Probably want this:
    Code:
    p = strtok(NULL, delim.c_str());
    (Or create a "const char *cdelim = delim.c_str();" and use that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Banned
    Join Date
    Nov 2007
    Posts
    678
    oops! right! i missed it on the second call to strtok! *embarrased*

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    My solution:
    Code:
    	StringVector Split(const Strings::CStringEx& strToSplit, const Strings::CStringEx& strSplitWhat)
    	{
    		ppnew< std::vector<Strings::CStringEx> > pVector;
    		UINT nLastIndex = 0;
    		INT nIndex = 0;
    		while (nIndex != -1)
    		{
    			nIndex = strToSplit.Find(strSplitWhat, nLastIndex);
    			if (nIndex > -1)
    			{
    				//Strings::CStringEx strTemp = strToSplit.Mid(nLastIndex, nIndex - nLastIndex - 1);
    				//const TCHAR* str = strTemp;
    				pVector->push_back( strToSplit.Mid(nLastIndex, nIndex - nLastIndex) );
    				nLastIndex = (UINT)nIndex + strSplitWhat.GetLength();
    			}
    		}
    		pVector->push_back( strToSplit.Mid(nLastIndex) );
    		return pVector;
    	}
    	typedef std::vector<Strings::CStringEx> StringVector;
    It should work on std::string, as well, if you could find proper functions to replace mid and find with.
    Or alternatively, I could public my string class once it's finished.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, I'll have a go.. Back in a few minutes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Banned
    Join Date
    Nov 2007
    Posts
    678
    geez! Mats!!
    i take my words *back*, the pure C++ version is more #h#o#r#r#i#b#l#e#

    anyways thanks sweet Elysia!

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Quote Originally Posted by manav View Post
    i take my words *back*, the pure C++ version is more #h#o#r#r#i#b#l#e#
    Horrible? This is exactly what strtok does, except that this doesn't destroy the input string.
    You only need to call the function instead of copying the code.
    I'll make it part of my String class sometime, but right now it's self-standing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Quote Originally Posted by matsp View Post
    Ok, I'll have a go.. Back in a few minutes.
    --
    Mats
    i will also go now, gotta watch a favorite TV show of mine!
    bye Elysia!

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    int main(int argc, char *argv[])
    {
        std::string data("111001001110111010101110111011101110111|"
    		     "101011000010001010101000100000101010101|"
    		     "101001001110011011101110111000101110111|"
    		     "101001001000001000100010101000101010001|"
    		     "111011101110111000101110111000101110111");
        std::vector<std::string> fontMap;
        size_t pos = 0;
        size_t oldpos = 0;
        
        for(;;) 
        {
          pos = data.find('|', oldpos);
          std::cout << "pos = " << pos << std::endl;
          if (pos == data.npos)
          {
    	 fontMap.push_back(data.substr(oldpos));
    	 break;
          }
          fontMap.push_back(data.substr(oldpos, pos-oldpos));
          oldpos = pos+1;
        }
        for(std::vector<std::string>::size_type i = 0; i < fontMap.size(); i++)
        {
           std::cout << fontMap[i] << std::endl;
        }
        return 0;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help, extracting substrings from a string
    By doubty in forum C Programming
    Replies: 1
    Last Post: 06-17-2009, 11:57 PM
  2. Need help on substrings and streams
    By TaiL in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2008, 06:18 PM
  3. Searching for a series of possible substrings inside a string
    By andrew.bolster in forum C Programming
    Replies: 7
    Last Post: 02-10-2008, 01:20 AM
  4. substrings
    By arjunajay in forum C++ Programming
    Replies: 30
    Last Post: 06-10-2005, 09:13 PM
  5. Searching strings - substring's
    By Vber in forum C Programming
    Replies: 4
    Last Post: 02-06-2003, 11:05 AM

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