2016-07-31 18 views
-4

から文字列抽出「を。」文字列のベクトルで Cでgetlineを使ってこれを行う方法+ +?私は各単語と入れたい文字列</p> <pre><code>My name is bob.I am fine </code></pre> <p>持つテキスト

編集: '' 私が欲しい

std::vector<std::string> words; 
std::string word; 
while (cin>> word) { 
    words.push_back(word); 
} 

私がすることができない別の文字列として。

+3

が宿題の質問のように見える動作するはずです。親切に[\ [what \]](http://stackoverflow.com/help/mcve)をご覧ください。 – sjsam

+1

これはあまり話題にはなりません。トピックの内容のリストについては、[ask]を参照してください。 –

答えて

0

は、最もエレガントな解決策が、これは

#include <iostream> 
#include <vector> 
#include <string> 
#include <sstream> 

using namespace std; 

vector<string> split(string str, char delimiter) 
{ 
    vector<string> internal; 
    stringstream ss(str); 
    string tok; 

    while(getline(ss, tok, delimiter)) 
    { 
     internal.push_back(tok); 
    } 

    return internal; 
} 

int main(int argc, char **argv) 
{ 

    string str = "My name is bob.I am fine"; 
    for(int i = 0; i < str.length(); i++) 
    { 
     if(str[i] == '.') 
     { 
      str.insert(i++," "); 
      str.insert(++i," "); 
     } 
    } 
    vector<string> sep = split(str, ' '); 


    for(string t : sep) 
     cout << t << endl; 
} 
関連する問題

 関連する問題