vector of substrings

This is a discussion on vector of substrings within the C++ Programming forums, part of the General Programming Boards category; Code: std::vector<std::wstring> split( const std::wstring& text, wchar_t delimiter ) { std::vector<std::wstring> result; std::wstring::size_type start = 0; std::wstring::size_type end = text.find( ...

  1. #16
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Code:
    std::vector<std::wstring> split( const std::wstring& text, wchar_t delimiter )
    {
    	std::vector<std::wstring> result;
    
    	std::wstring::size_type start = 0;
    	std::wstring::size_type end   = text.find( delimiter, start );
    
    	while( end != wstring::npos )
    	{
    		std::wstring token = text.substr( start, end - start );
    
    		result.push_back( token );
    
    		start = end + 1;
    		end   = text.find( delimiter, start );
    	}
    
    	result.push_back( text.substr( start ) );
    
    	return result;
    }
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nvoigt View Post
    Code:
    std::vector<std::wstring> split( const std::wstring& text, wchar_t delimiter )
    {
    	std::vector<std::wstring> result;
    
    	std::wstring::size_type start = 0;
    	std::wstring::size_type end   = text.find( delimiter, start );
    
    	while( end != wstring::npos )
    	{
    		std::wstring token = text.substr( start, end - start );
    
    		result.push_back( token );
    
    		start = end + 1;
    		end   = text.find( delimiter, start );
    	}
    
    	result.push_back( text.substr( start ) );
    
    	return result;
    }
    Yeah, that's better than mine.

    --
    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. #18
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Why not use boost::tokenizer?

  4. #19
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It seems that stringstream could be used too.

    Code:
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <iterator>
    
    void break_up(const std::string& s, char at, std::vector<std::string>& out)
    {
        std::stringstream stream(s);
        std::string word;
        while (getline(stream, word, at)) {
            out.push_back(word);
        }
    }
    
    
    int main(int argc, char *argv[])
    {
        std::string data("111001001110111010101110111011101110111|101011000010001010101000100000101010101|"
                         "101001001110011011101110111000101110111|101001001000001000100010101000101010001|1"
                         "11011101110111000101110111000101110111");
        std::vector<std::string> fontMap;
        break_up(data, '|', fontMap);
        std::copy(fontMap.begin(), fontMap.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
        return 0;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #20
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Thanks all, for such nice & neat examples

  6. #21
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Dang!
    This is just 31 lines Python code, which is driving me nuts, when I am trying to do it in C++ *_*

    With the help of you people, I figured that how to do this simple thing in Pyhton, in C++:
    fontMap='101010|001010|001011'.split('|')

    Now I am again stuck, actually not really out of ideas, since, I honestly admit, that I can write the same program in asm also (I have done, similar stuff before), but, just seeing how convoluted it becomes, even when, I am trying to use a really high level language!

    There is my code, for curious, and for those C++ legends, which will help me do the same code in C++, but in below 50 lines (I hope):
    Code:
    # aa.py
    
    import sys
    
    fontMap = "111001001110111010101110111011101110111|"\
    "101011000010001010101000100000101010101|"\
    "101001001110011011101110111000101110111|"\
    "101001001000001000100010101000101010001|"\
    "111011101110111000101110111000101110111".split('|')
    
    W=4
    
    if len(sys.argv) != 2:
    	print "Usage\n\taa.py <number>"
    	sys.exit(1)
    else:
    	number = sys.argv[1]
    
    
    for row in fontMap:
    	line = ''
    	for d in number:
    		if d not in '0123456789':
    			print 'Error: Only digits allowed!'
    			sys.exit(1)
    		i1 = int(d) * W
    		i2 = i1 + W
    		line += row[i1:i2]
    		if d == '9': line += ' '
    	line = line.replace('0', ' ')
    	line = line.replace('1', '@')
    	print line
    
    sys.exit(0)
    Last edited by manav; 05-10-2008 at 04:30 AM.

  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Using anon's function:

    Code:
    std::vector<std::string> fontMap;
    break_up(data, '|', fontMap);
    for (int i = 0; i < fontMap.size(); i++)
    {
        std::string line = fontMap[i];
    }
    I don't understand what the code (your python) is supposed to do, though, so... >_<
    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.

  8. #23
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Yeah, I know that, anon, you, Mats and others gave me the solution, for, how to split a string into an substrings array.
    Code:
    fontMap = "111001001110111010101110111011101110111|"\
    "101011000010001010101000100000101010101|"\
    "101001001110011011101110111000101110111|"\
    "101001001000001000100010101000101010001|"\
    "111011101110111000101110111000101110111".split('|')
    
    So the above part is taken care of!

    Now, leave the argv check and usage part, and come to the for ... for (nested) loops.
    I could use simple facilities of Python which are sorely lacking in vector, string etc in C++!
    Double dang!! So much for the efficiency of C++

  9. #24
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    STL's classes are lacking and difficult to use
    Still in the dark about what you're trying to do, however.
    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.

  10. #25
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Quote Originally Posted by Elysia View Post
    STL's classes are lacking and difficult to use
    Still in the dark about what you're trying to do, however.
    Well I am trying to print numbers, given on command line, in this format:
    Code:
    @@@  @  @@@ @@@ @ @ @@@ @@@ @@@ @@@ @@@ 
    @ @ @@    @   @ @ @ @   @     @ @ @ @ @ 
    @ @  @  @@@  @@ @@@ @@@ @@@   @ @@@ @@@ 
    @ @  @  @     @   @   @ @ @   @ @ @   @ 
    @@@ @@@ @@@ @@@   @ @@@ @@@   @ @@@ @@@
    By the way my data in fontMap is put in that way, so as to avoid detection, of what the heck is in fontMap

  11. #26
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Well, how about something like:

    Code:
    std::vector<Strings::CStringEx> fontMap;
    break_up(data, '|', fontMap);
    for (int i = 0; i < fontMap.size(); i++)
    {
        Strings::CStringEx line = fontMap[i];
        line.Replace("1", "@");
        line.Replace("0", " ");
        for (uint32_t i = 0; i < line.GetLength(); i++)
            cout << line[i];
        cout << "\n";
    }
    =)
    Or replace with your QString or whatever.
    std::string is too lacking.
    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.

  12. #27
    Banned
    Join Date
    Nov 2007
    Posts
    678
    MuwaahhahhahHahaHAHhahHAAHAHhahahhhaaaaa!
    You are so unbelievably *silly* X-(

    That is not, not by faaaaaaaaaaar close, to, what I was doing!!!

  13. #28
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Duh, I don't understand python.
    Plus you're making is far too complex - far more than it needs to be.
    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. #29
    Banned
    Join Date
    Nov 2007
    Posts
    678
    Hurray! I made it, in C, less than 50 lines of code
    Actually Python has indentation rule advantage, but in C only braces are taking 12 lines, here is my code at 49 lines (49 - 12 = 37, not bad, just six more than Python version)

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    const char *fontMap[] = {
    "1110010011101110101011101110111011101110",
    "1010110000100010101010001000001010101010",
    "1010010011100110111011101110001011101110",
    "1010010010000010001000101010001010100010",
    "1110111011101110001011101110001011101110",
    };
    
    const int FONT_WIDTH = 4;
    const int FONT_HEIGHT = 5;
    
    int main(int argc, char *argv[])
    {
        const char *number;
        int row, col, len, i1, i2;
        
        if (argc != 2) 
        {
            printf("Usage\n\taa.py <number>\n");
            return 1;
        }
        number = argv[1];
        len = strlen(number);
        for (row=0; row<FONT_HEIGHT; row++) 
        {
            for (col=0; col<len; col++) 
            {
                if (! isdigit(number[col]))
                {
                    printf("Error: Only digits allowed!\n");
                    return 1;
                }
                i1 = (number[col]-'0') * FONT_WIDTH;
                i2 = i1 + FONT_WIDTH;
                while (i1 < i2)
                {
                    putchar(fontMap[row][i1]=='0' ? ' ' : '@');
                    i1++;
                }
            }
            putchar('\n');
        }
        return 0;
    }
    Last edited by manav; 05-10-2008 at 05:55 AM. Reason: Friends can *kill* you for your good! Listen to Them!!!

  15. #30
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    I shouldn't have to say this to you, but... Use better variable names!
    And here's another one for you: http://cpwiki.sourceforge.net/Common...kes_and_errors
    Last edited by Elysia; 05-10-2008 at 05:38 AM.
    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.

Page 2 of 4 FirstFirst 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