Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Общие вопросы > Выделение подстроки


Автор: Vredniy 16.10.2007, 17:39
Привет я новенький,не поможите с проблемой smile

Хочу удалить пробелы из строки но не получается :(

Код

#include "stdafx.h"
#include <iostream>
#include <string>
#include <string.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    string s1 = "aaa bbb  ccc   ddd    eee      fff"; 
    char chars[] = {' '};
    string s2[] = s1.split( chars );
    for(int i=0;i<6;i++)
    {
         cout << s2[i]<<endl;
    }
    return 0;
}

Автор: _stranger_ 16.10.2007, 19:13
Вот один из вариантов, попробуй, может подойдет:
Код


#include <iostream>
#include <algorithm>
#include <string>

int main()
{
    std::string s2,s1 = "aaa bbb  ccc   ddd    eee      fff"; 
    std::string::iterator iter1(s1.begin()),end1(s1.end()),iter2(s1.begin());
    while(iter1 != end1)
    {
       iter1 = std::find(iter1,end1,' ');
       s2.append(std::string(iter2,iter1));
       if(iter1 != end1)
       {
         iter2 = ++iter1;
       }
    }
    std::cout << s2 << std::endl;
    return 0;
}

Автор: FREEDEV 16.10.2007, 19:41
Код

#include <algorithm>
#include <string>
#include <conio.h>
            
string s1 = "aaa bbb  ccc   ddd    eee      fff"; 
while(s1.erase(find_if(s1.begin(),s1.end(),isspace)) != s1.end());
cout << s1 << endl;
getch();

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)