C++ While 循环范围问题
C++ While loop scoping issue
我最近开始学习 C++,目前正在尝试构建我最近在 python 中构建的工具。我的问题是我无法弄清楚如何使单词列表的长度全局化,例如:
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char* argv[]) {
....
fstream file;
file.open(argv[i], ios::in);
if (file.is_open()) {
string tp;
while (getline(file, tp)) {
// cout << tp << "\n" << endl;
string words[] = {tp};
int word_count = sizeof(words) / sizeof(words[0]);
for (int e = 0; e < word_count; e++) {
cout << word_count[e];
}
}
file.close();
} else {
cout << "E: File / Directory " << argv[i] << " Does not exist";
return 0;
}
....
}
上面写着 int word_count = sizeof(words) / sizeof(words[0]);
和 tring words[] = {tp};
的地方,我希望能够使用那个全局变量,这样我以后就可以使用数组的长度和数组本身,这样我可以遍历它并在另一个语句中使用它们。
谁能告诉我怎么做?
顺便说一句,我只用了大约 4 天的时间来学习 C++,所以如果我不明白你告诉我的内容,请不要生气。
string words[] = {tp};
创建一个数组,其中包含一个元素,即整行。名称 words
意味着您想要存储单个单词。如果你想在循环完成后使用它,范围也是错误的。您需要在循环之前声明它。使用 std::vector<std::string>
来存储单词。它可能看起来像这样:
#include <vector>
int main(int argc, char* argv[]) {
std::vector<std::string> words;
// ...
if (file) {
std::string word;
while (file >> word) { // read one word
words.push_back(word); // store it in the vector<string>
}
std::cout << "word count: " << words.size() << '\n';
// print all the words in the vector:
for(std::string& word : words) {
std::cout << word << '\n';
}
// file.close() // not needed, it'll close automatically when it goes out of scope
}
您这里有很多问题。首先,不是在循环的每次迭代中将 tp
添加到名为 words
的数组中,您实际上是在创建一个新数组 words
并将 tp
分配给第一个元素的阵列。 words
在 while 循环的每次迭代结束时被销毁,但即使没有被销毁,{tp}
表达式也会覆盖数组中已有的内容。因此,您计算长度的行将始终将长度计算为 1。
接下来,word_count[e]
试图读取名为 word_count
的数组的元素 e
,但 word_count
实际上是一个整数。如果这真的编译了,我会感到惊讶。
首先,我建议您使用 std::vector
,然后如果您只是将其范围扩大到循环之外,那么您就可以在其他地方使用它。例如:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main(int argc, char* argv[]) {
fstream file;
file.open(argv[i], ios::in);
std::vector<std::string> words;
if (file.is_open()) {
string tp;
while (getline(file, tp)) {
words.push_back(tp); // Add tp to the vector of words
}
int word_count = words.size(); // vectors 'know' their own size.
for (int e = 0; e < word_count; e++) {
cout << words[e];
}
file.close();
} else {
cout << "E: File / Directory " << argv[i] << " Does not exist";
return 0;
}
}
// You can now use words as you please, because it is still in scope.
}
我最近开始学习 C++,目前正在尝试构建我最近在 python 中构建的工具。我的问题是我无法弄清楚如何使单词列表的长度全局化,例如:
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char* argv[]) {
....
fstream file;
file.open(argv[i], ios::in);
if (file.is_open()) {
string tp;
while (getline(file, tp)) {
// cout << tp << "\n" << endl;
string words[] = {tp};
int word_count = sizeof(words) / sizeof(words[0]);
for (int e = 0; e < word_count; e++) {
cout << word_count[e];
}
}
file.close();
} else {
cout << "E: File / Directory " << argv[i] << " Does not exist";
return 0;
}
....
}
上面写着 int word_count = sizeof(words) / sizeof(words[0]);
和 tring words[] = {tp};
的地方,我希望能够使用那个全局变量,这样我以后就可以使用数组的长度和数组本身,这样我可以遍历它并在另一个语句中使用它们。
谁能告诉我怎么做? 顺便说一句,我只用了大约 4 天的时间来学习 C++,所以如果我不明白你告诉我的内容,请不要生气。
string words[] = {tp};
创建一个数组,其中包含一个元素,即整行。名称 words
意味着您想要存储单个单词。如果你想在循环完成后使用它,范围也是错误的。您需要在循环之前声明它。使用 std::vector<std::string>
来存储单词。它可能看起来像这样:
#include <vector>
int main(int argc, char* argv[]) {
std::vector<std::string> words;
// ...
if (file) {
std::string word;
while (file >> word) { // read one word
words.push_back(word); // store it in the vector<string>
}
std::cout << "word count: " << words.size() << '\n';
// print all the words in the vector:
for(std::string& word : words) {
std::cout << word << '\n';
}
// file.close() // not needed, it'll close automatically when it goes out of scope
}
您这里有很多问题。首先,不是在循环的每次迭代中将 tp
添加到名为 words
的数组中,您实际上是在创建一个新数组 words
并将 tp
分配给第一个元素的阵列。 words
在 while 循环的每次迭代结束时被销毁,但即使没有被销毁,{tp}
表达式也会覆盖数组中已有的内容。因此,您计算长度的行将始终将长度计算为 1。
接下来,word_count[e]
试图读取名为 word_count
的数组的元素 e
,但 word_count
实际上是一个整数。如果这真的编译了,我会感到惊讶。
首先,我建议您使用 std::vector
,然后如果您只是将其范围扩大到循环之外,那么您就可以在其他地方使用它。例如:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main(int argc, char* argv[]) {
fstream file;
file.open(argv[i], ios::in);
std::vector<std::string> words;
if (file.is_open()) {
string tp;
while (getline(file, tp)) {
words.push_back(tp); // Add tp to the vector of words
}
int word_count = words.size(); // vectors 'know' their own size.
for (int e = 0; e < word_count; e++) {
cout << words[e];
}
file.close();
} else {
cout << "E: File / Directory " << argv[i] << " Does not exist";
return 0;
}
}
// You can now use words as you please, because it is still in scope.
}