2013-07-30 47 views
1

對不起,如果這是一個重複的問題,但我已經試圖尋找答案並空手而來。所以基本上我只想將字符串(單個單詞)添加到矢量的後面,然後將存儲的字符串顯示爲單個字符串。我是新秀。在C++中顯示一個向量字符串

#include <iostream> 
#include <vector> 
#include <string> 
#include <cctype> 
using namespace std; 


int main(int a, char* b []) 
{ 
    vector<string> userString;  
    string word;   
    string sentence = "";   
    for (decltype(userString.size()) i = 0; i <= userString.size() - 1; i++) 
    { 
     cin >> word; 
     userString.push_back(word); 
     sentence += userString[i] + " "; 
    } 
    cout << sentence; 
    system("PAUSE"); 
    return 0; 
} 

爲什麼不能正常工作?

編輯

int main(int a, char* b []) 
{ 
    cout << "Enter a sequence of words. Enter '.' \n"; 
    vector<string> userString;  
    string word;      
    string sentence = "";   /
    int wordCount = 0; 
    while (getline(cin, word)) 
    { 
     if (word == ".") 
     { 
      break; 
     } 
     userString.push_back(word); 
    } 
    for (decltype(userString.size()) i = 0; i <= userString.size() - 1; i++) 
    { 
     sentence += userString[i] + " "; 
     wordCount += 1; 
     if (wordCount == 8) 
     { 
      sentence = sentence + "\n"; 
        wordCount = 0; 
     } 
    } 
    cout << sentence << endl; 
    system("PAUSE"); 
    return 0; 
} 

所以我的新節目工作。它只是將值放在矢量的後面,並將它們打印出8個單詞。我知道有更簡單的方法,但我只是學習矢量,而且我正在採取嬰兒步驟。謝謝你們的幫助。

+1

「謝謝你們的幫助」,那你爲什麼不標記「答案」呢? – JackBauer

+1

那麼這裏接受的答案是什麼? – Nogurenn

回答

2

您的vector<string> userString尺寸爲0,因此不會輸入循環。你可以在給定大小的矢量開始:

vector<string> userString(10);  
string word;   
string sentence;   
for (decltype(userString.size()) i = 0; i < userString.size(); ++i) 
{ 
    cin >> word; 
    userString[i] = word; 
    sentence += userString[i] + " "; 
} 

,雖然目前尚不清楚爲什麼你需要的載體都:

string word;   
string sentence;   
for (int i = 0; i < 10; ++i) 
{ 
    cin >> word; 
    sentence += word + " "; 
} 

如果你不希望有一個固定的限制輸入字的數量,您可以在while循環中使用std::getline,檢查某個輸入,例如"q"

while (std::getline(std::cin, word) && word != "q") 
{ 
    sentence += word + " "; 
} 

,直到你輸入 「Q」 這將增加的話sentence

+0

要注意,即使它有一個大小,它不會工作,因爲他正在做push_backs而不是指數分配。 – Borgleader

+0

@Borgleader當然,必須通過'operator []'來分配。 – juanchopanza

+0

謝謝你指出我試圖分配給一個空向量。我使用矢量的原因是因爲我在教導自己,而我剛剛達到了矢量的主題。我很清楚,有一個更簡單的方法來做到這一點。順便說一句,你提醒我getline()幫助我超出了你的想象。 – RudolphRedNose

4

因爲userString是空的。你只聲明它

vector<string> userString;  

但從來沒有添加任何東西,所以for循環甚至不會運行。

0

vector.size()返回一個向量的大小。你沒有在循環中放入任何字符串,所以矢量的大小是0.它永遠不會進入循環。首先將一些數據放入向量中,然後嘗試添加它們。您可以從用戶輸入用戶想要輸入的字符串數量。

#include <iostream> 
#include <vector> 
#include <string> 
#include <cctype> 
using namespace std; 


int main(int a, char* b []) 
{ 
    vector<string> userString; 
    string word; 
    string sentence = ""; 
    int SIZE; 
    cin>>SIZE; //what will be the size of the vector 
    for (int i = 0; i < SIZE; i++) 
    { 
     cin >> word; 
     userString.push_back(word); 
     sentence += userString[i] + " "; 
    } 
    cout << sentence; 
    system("PAUSE"); 
    return 0; 
} 

另一件事,實際上你不必使用矢量來做到這一點。兩個字符串可以爲你做這項工作。

#include <iostream> 
#include <vector> 
#include <string> 
#include <cctype> 
using namespace std; 


int main(int a, char* b []) 
{ 
    // vector<string> userString; 
    string word; 
    string sentence = ""; 
    int SIZE; 
    cin>>SIZE; //what will be the size of the vector 
    for (int i = 0; i < SIZE; i++) 
    { 
     cin >> word; 
     sentence += word+ " "; 
    } 
    cout << sentence; 
    system("PAUSE"); 
    return 0; 
} 

,如果你想輸入的字符串,直到用戶的願望,代碼將是這樣的:

#include <iostream> 
#include <vector> 
#include <string> 
#include <cctype> 
using namespace std; 


int main(int a, char* b []) 
{ 
    // vector<string> userString; 
    string word; 
    string sentence = ""; 
    //int SIZE; 
    //cin>>SIZE; //what will be the size of the vector 
    while(cin>>word) 
    { 
     //cin >> word; 
     sentence += word+ " "; 
    } 
    cout << sentence; 
    // system("PAUSE"); 
    return 0; 
} 
+0

爲什麼要從用戶那裏讀取大小,爲什麼不直接讀取字符串,直到用戶停止輸入它們?爲什麼你的變量喊? 'size'比'SIZE'更好的名字 –

2

你必須使用存在於載體STL的插入方法插入的元素,檢查下面的程序添加元素到它,你可以在你的程序中以相同的方式使用。

#include <iostream> 
#include <vector> 
#include <string.h> 

int main() 
{ 
    std::vector<std::string> myvector ; 
    std::vector<std::string>::iterator it; 

    it = myvector.begin(); 
    std::string myarray [] = { "Hi","hello","wassup" }; 
    myvector.insert (myvector.begin(), myarray, myarray+3); 

    std::cout << "myvector contains:"; 
    for (it=myvector.begin(); it<myvector.end(); it++) 
    std::cout << ' ' << *it; 
    std::cout << '\n'; 

    return 0; 
} 
1

你問了兩個問題;你的標題顯示「顯示一個字符串矢量」,但你實際上並沒有這樣做,你實際上會構建一個由所有字符串組成的字符串並輸出它。

你的問題主體詢問「爲什麼不工作」。

它不起作用,因爲您的for循環受到「userString.size()」爲0的限制,並且您測試的循環變量爲「userString.size() - 1」。 for()循環的條件在允許執行第一次迭代之前進行測試。

int n = 1; 
for (int i = 1; i < n; ++i) { 
    std::cout << i << endl; 
} 

將完全沒有打印。

因此,你的循環執行完全沒有迭代,留下userString和句子爲空。

最後,你的代碼絕對沒有理由使用一個向量。在聲稱自己是菜鳥的同時,你使用「decltype(userString.size())」而不是「size_t」或「auto」的事實表明你要麼從頭到尾地閱讀一本書,要麼你正在設置自己使班級失敗。

所以要在你的文章結尾回答你的問題:它不起作用,因爲你沒有用調試器對它進行檢查,並檢查它的值。雖然我說得很直白,但我會把它留在那裏。

+0

謝謝我知道它可以以另一種可能的方式完成,但我在教導我自己,我剛開始使用矢量,這是一種練習。由於你和他人的意見,我已經完成了我的任務,所以我感謝你。 – RudolphRedNose

相關問題