2012-04-29 5 views
1

に空白文字で文字列を解析では、私は、この文字列を分割し、私のベクトルの異なるスロットにすべての番号を配置したい数字ベクトル

"1 2 3 4 5 6" 

の文字列があるとします。これはに空の文字列をプッシュすることはありませんという利点があり、この

+2

Stringstreamsと 'getline'は、このために働きます。 – chris

+3

std :: stringstream mystringstream(mystring); std :: copy(std :: istream_iterator (mystringstream)、std :: istream_iterator ()、std :: back_inserter(myvector)); –

+0

[C++で文字列を分割する方法](http://stackoverflow.com/questions/236129) – Blastfurnace

答えて

0
#include <iostream> 
#include <string> 
#include <algorithm> 
#include <cstdlib> 

std::vector<std::string> StringToVector(std::string const& str, char const delimiter); 

int main(){ 

    std::string str{"1 2  3 4 5 6 "}; 
    std::vector<std::string> vec{StringToVector(str, ' ')}; 


    //print the vector 
    for(std::string const& item : vec){ 
     std::cout << "[" << item << "]"; 
    } 


    return EXIT_SUCCESS; 
} 

std::vector<std::string> StringToVector(std::string const& str, char const delimiter){ 

    std::vector<std::string> vec; 
    std::string element; 


    //we are going to loop through each character of the string slowly building an element string. 
    //whenever we hit a delimiter, we will push the element into the vector, and clear it to get ready for the next element 
    for_each(begin(str),end(str),[&](char const ch){ 
     if(ch!=delimiter){ 
      element+=ch; 
     } 
     else{ 
      if (element.length()>0){ 
      vec.push_back(element); 
      element.clear(); 
      } 
     } 
    }); 


    //push in the last element if the string does not end with the delimiter 
    if (element.length()>0){ 
     vec.push_back(element); 
    } 


    return vec; 
} 

グラム++ -std = C++ 0xの-oメインmain.cppに

について移動する最良の方法は何ですかベクター。
セパレータをどのようにするかを選択することもできます。
多分あなたは他のものを書くことができます:文字ベクトルのためのものか、あるいは区切り文字が文字列かもしれませんか? :)
幸運!

3

文字列をストリームとして参照するにはistringstreamを使用し、番号を取得するには>>演算子を使用します。文字列に改行とタブが含まれていても動作します。次に例を示します。

#include <vector> 
#include <sstream> // for istringstream 
#include <iostream> // for cout 

using namespace std; // I like using vector instead of std::vector 

int main() 
{ 
    char *s = "1 2 3 4 5"; 
    istringstream s2(s); 
    vector<int> v; 
    int tmp; 

    while (s2 >> tmp) { 
    v.push_back(tmp); 
    } 

    // print the vector 
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) { 
    cout << *it << endl; 
    } 

} 
+1

whileループを改善することができます。 'while(s2)'を 'while(s2 >> tmp)'に変更すると、ループ内のブレークを削除することもできます。 –

+0

@Loki Astari - ありがとう、完了。 –

+0

問題ありません。注:ベクターを印刷しているだけの場合。その後、実際にメンバーにアクセスする必要はありません。したがって、 'ベクトル :: const_iterator'を使う価値があります –

0
#include <vector> 
#include <string> 
#include <sstream> 
int str_to_int(const string& str){ 
    stringstream io; 
    int out; 
    io<<str; 
    io>>out; 
    return out; 
}; 

vector<int> Tokenize(string str, string delimiters = " ") 
{ 
    vector<int> tokens; 
    string::size_type nwpos; //position of first non white space, which means it is  first real char 
    nwpos = str.find_first_not_of(delimiters, 0); //ignore the whitespace before the first word 

    string::size_type pos = str.find_first_of(delimiters, nwpos); 

    while (string::npos != pos || string::npos != nwpos) 
    { 
     // Found a token, add it to the vector. 
     tokens.push_back(str_to_int(str.substr(nwpos, pos - nwpos))); 
     // Skip delimiters. Note the "not_of" 
     nwpos = str.find_first_not_of(delimiters, pos); 
     // Find next "non-delimiter" 
     pos = str.find_first_of(delimiters, nwpos); 
    } 
    return tokens; 
}; 
0

試してみてください。

#include <sstream> 
#include <string> 
#include <algorithm> 
#include <iterator> 
#include <vector> 

int main() 
{ 
    // The data 
    std::string data = "1 2 3 4 5 6"; 

    // data in a stream (this could be a file) 
    std::stringstream datastream(data); 

    // Copy the data from the stream into a vector. 
    std::vector<int> vec; 
    std::copy(std::istream_iterator<int>(datastream), std::istream_iterator<int>(), 
       std::back_inserter(vec) 
      ); 


    // We can also copy the vector to the output (or any other stream). 
    std::copy(vec.begin(), vec.end(), 
       std::ostream_iterator<int>(std::cout, "\n") 
      ); 
}