我正在嘗試從輸入文本中創建一個隨機密碼。最小長度為 3。我發現 The 第一個單詞的大小為 6。到目前為止,只有第一個詞給了我奇怪的尺寸。最終,當單詞少于 3 個單詞時,我想擦除。我不知道為什么它回傳大小 6。請指教。
void setMinLength(std::vector<std::string> &words) {
for (int i = 0; i < words.size()-1; i ) {
if (words[i].size() == 6) {
std::cout << words[i] << std::endl;
//words.erase(words.begin() i);
}
}
}
int main() {
std::ifstream myFile("input.txt");
if (!myFile.is_open()) {
std::cout << "Couldn't open the file.";
return 0;
}
std::vector<std::string> words;
std::string word;
while (myFile >> word) {
words.push_back(word);
}
setMinLength(words);
myFile.close();
return 0;
}
Input.text 檔案如下。
The Project Gutenberg EBook of Grimms’ Fairy Tales, by The Brothers Grimm This eBook is for the use of anyone anywhere at no cost and with almost no restrictions whatsoever. You may copy it, give it away or
The
Tales,
anyone
almost
re-use
online
Title:
Taylor
Marian
十六進制編輯器
uj5u.com熱心網友回復:
首先,在您輸入的文本檔案中,有位元組順序標記,因此它會影響單詞的大小。洗掉那個。
在你的setMinLength(std::vector<std::string> &words)函式中。
void setMinLength(std::vector<std::string> &words) {
for (int i = 0; i < words.size()-1; i ) {
if (words[i].size() == 6) {
std::cout << words[i] << std::endl;
//words.erase(words.begin() i);
// --i; explain below
}
}
}
請注意,如果您使用erase(): 當您使用erase(),words[i]現在是下一個單詞,然后回圈遞增i并跳過一個單詞。記得--i在最后。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/339991.html
