строки в C++ срочно

Discussion in 'С/С++, C#, Rust, Swift, Go, Java, Perl, Ruby' started by 77org77, 15 Jun 2010.

  1. 77org77

    77org77 New Member

    Joined:
    31 May 2010
    Messages:
    11
    Likes Received:
    0
    Reputations:
    0
    Привет всем у меня такой вопрос по C++ Ксть какая небуть сандартная функция чтобы поделить строку по кусочкам например строка Имя|фамилия|Отчество|Дата и всё это разделить например на массивы.
     
  2. Ra$cal

    Ra$cal Elder - Старейшина

    Joined:
    16 Aug 2006
    Messages:
    670
    Likes Received:
    185
    Reputations:
    78
    нету
     
  3. 77org77

    77org77 New Member

    Joined:
    31 May 2010
    Messages:
    11
    Likes Received:
    0
    Reputations:
    0
    Жалко а как можно поделить строку
     
  4. Ins3t

    Ins3t Харьковчанин

    Joined:
    18 Jul 2009
    Messages:
    939
    Likes Received:
    429
    Reputations:
    139
    Разбить вашу строку можно сишной функцией strtok библиотеки string.h

    Пример использования strtok.

    Code:
    //////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////
    #include <iostream>
    using std::cout;
    using std::endl;
    //////////////////////////////////////////////////////////////////////////
    #include <cstring>
    using std::strtok;
    //////////////////////////////////////////////////////////////////////////
    int main( void )
    	{
    	char sentence[] = "Name|First Name|Blabla|lalala";
    	char *tokenPtr;
    
    	tokenPtr = strtok( sentence, "|" );
    
    	while( tokenPtr != NULL )
    		{
    		cout << tokenPtr << endl;
    		tokenPtr = strtok( NULL, "|" );
    		}
    
    	return 0;
    	}
    //////////////////////////////////////////////////////////////////////////
    
    Или с помощью библиотеки С++ string:

    Code:
    //////////////////////////////////////////////////////////////////////////
    #include <iostream>
    using std::cout;
    using std::endl;
    //////////////////////////////////////////////////////////////////////////
    #include <string>
    using std::string;
    //////////////////////////////////////////////////////////////////////////
    int main( void )
    	{
    	string myString( "Name|First Name|Blabla|lalala" );
    	size_t found;
    
    	found = myString.find_first_of( "|" );
    
    	while( found != string::npos )
    		{
    		myString[ found ] = '\n';
    		found = myString.find_first_of( "|", found + 1 );
    		}
    
    	cout << myString << endl;
    
    	return 0;
    	}
    //////////////////////////////////////////////////////////////////////////
    
     
    #4 Ins3t, 15 Jun 2010
    Last edited: 15 Jun 2010
    2 people like this.
  5. Самовар

    Joined:
    15 Jun 2009
    Messages:
    219
    Likes Received:
    13
    Reputations:
    0
    Блин, а мне так нравилась explode из PHP
     
  6. Ra$cal

    Ra$cal Elder - Старейшина

    Joined:
    16 Aug 2006
    Messages:
    670
    Likes Received:
    185
    Reputations:
    78
    http://blog.vernart.ru/programming/cplusplus/135/
    в плюсах тож не сложно, если буст юзать.