嘗試下面的例子。 您的第三個詞是std :: vector中的第3個項目...
創建一個拆分字符串函數,將大字符串拆分爲std :: vector對象。使用該std :: vector來獲取第三個字符串。
請看下面的例子,嘗試在一個空的C++控制檯項目運行。
#include <stdio.h>
#include <vector>
#include <string>
void splitString(std::string str, char token, std::vector<std::string> &words)
{
std::string word = "";
for(int i=0; i<str.length(); i++)
{
if (str[i] == token)
{
if(word.length() == 0)
continue;
words.push_back(word);
word = "";
continue;
}
word.push_back(str[i]);
}
}
int main(int argc, char **argv)
{
std::string stream = "word1\tword2\tword3\tword4\tword5\tword6";
std::vector<std::string> theWords;
splitString(stream, '\t', theWords);
for(int i=0; i<theWords.size(); i++)
{
printf("%s\n", theWords[i].c_str());
}
while(true){}
return 0;
}
那麼,你嘗試過這麼遠嗎?你必須嘗試一些東西。你卡在哪裏?你顯然知道什麼是分開的話。你是如何尋找這些的? – Bart 2012-07-13 12:25:32
這是第三次或多或少地提出相同的問題......只是使用例如[這個答案](http://stackoverflow.com/a/11469613/214671)你已經收到!編輯:或者稍微修改一下[我上次爲你寫的代碼](http://stackoverflow.com/a/11362616/214671)。 – 2012-07-13 12:27:50
你沒有向我們展示你所嘗試過的。給我們看一看。你卡在哪裏?看起來你解決了你以前的問題,這應該可以幫助你。 – Bart 2012-07-13 12:27:51