2012-04-17 9 views
6

Mam currentLine String = "12 23 45"tokenize ciąg w C++

muszę wyodrębnić 12, 23, 45 z tego łańcucha bez użycia bibliotek boost. Ponieważ używam napisu, strtok zawodzi dla mnie. Próbowałem wielu rzeczy, które wciąż nie przyniosły sukcesu.

Oto moja ostatnia próba

while(!inputFile.eof()) 
    while(getline(inputFile,currentLine)) 
     { 
      int countVar=0; 
      int inputArray[10]; 
      char* tokStr; 
      tokStr=(char*)strtok(currentLine.c_str()," "); 

      while(tokstr!=NULL) 
      { 
      inputArray[countVar]=(int)tokstr; 
      countVar++; 
      tokstr=strtok(NULL," "); 
      } 
     } 
} 

jeden bez strtok

string currentLine; 
while(!inputFile.eof()) 
    while(getline(inputFile,currentLine)) 
     { 
      cout<<atoi(currentLine.c_str())<<" "<<endl; 
      int b=0,c=0; 
      for(int i=1;i<currentLine.length();i++) 
       { 
        bool lockOpen=false; 
        if((currentLine[i]==' ') && (lockOpen==false)) 
         { 
         b=i; 
         lockOpen=true; 
         continue; 
         } 
        if((currentLine[i]==' ') && (lockOpen==true)) 
         { 
         c=i; 
         break; 
         } 
       } 
      cout<<b<<"b is"<<" "<<c;  
     } 
+0

Czy to Twój prawdziwy kod, z niedopasowanymi literami 'tokstr' i' tokStr'? Ponadto, http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c –

+0

Nie zgłaszasz, w jaki sposób zawodzą. Czy oni się kompilują? Czy się rozbijają? Czy dają nieprawidłowe wyniki? –

+0

USe >> operator z argumentem całkowitym. –

Odpowiedz

9

Spróbuj tego:

#include <sstream> 

std::string str = "12 34 56"; 
int a,b,c; 

std::istringstream stream(str); 
stream >> a >> b >> c; 

Czytaj dużo o C++ strumieni tutaj: http://www.cplusplus.com/reference/iostream/

+0

error: zmienna 'std :: istring stream' ma inicjator, ale niekompletny typ – CodeMonkey

+0

@ user1290495, '#include ' – hmjd

5
std::istringstream istr(your_string); 

std::vector<int> numbers; 
int number; 
while (istr >> number) 
    numbers.push_back(number); 

Albo prościej (choć naprawdę nie krótszy):

std::vector<int> numbers; 
std::copy(
    std::istream_iterator<int>(istr), 
    std::istream_iterator<int>(), 
    std::back_inserter(numbers)); 

(Wymaga standardowe nagłówki., <algorithm> i <iterator>)

0

Można również zdecydować się na Boost, tokenizera ......

#include <iostream> 
#include <string> 
#include <boost/foreach.hpp> 
#include <boost/tokenizer.hpp> 
using namespace std; 
using namespace boost; 

int main(int argc, char** argv) 
{ 
    string str= "India, gold was dear"; 
    char_separator<char> sep(", "); 
    tokenizer< char_separator<char> > tokens(str, sep); 
    BOOST_FOREACH(string t, tokens) 
    { 
     cout << t << "." << endl; 
    } 
} 
+1

"bez użycia bibliotek Boost" – k06a

0

stringstream i boost::tokenizer to dwie możliwości. Oto bardziej jednoznaczne rozwiązanie z użyciem string::find i string::substr.

std::list<std::string> 
tokenize(
    std::string const& str, 
    char const token[]) 
{ 
    std::list<std::string> results; 
    std::string::size_type j = 0; 
    while (j < str.length()) 
    { 
    std::string::size_type k = str.find(token, j); 
    if (k == std::string::npos) 
     k = str.length(); 

    results.push_back(str.substr(j, k-j)); 
    j = k + 1; 
    } 
    return results; 
} 

Mam nadzieję, że to pomoże. Możesz łatwo przekształcić to w algorytm, który zapisuje tokeny do dowolnych kontenerów lub bierze uchwyt funkcji, który przetwarza tokeny.