gpt4 book ai didi

c++ - 将整数添加到结构

转载 作者:行者123 更新时间:2023-11-30 05:34:50 26 4
gpt4 key购买 nike

我有一个像下面这样的错误

"incompatible types in assignment of int to int [10000]"

我不明白这是怎么回事。这是我的代码:

#include<fstream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

struct words{

string lexis;
int sizes[10000];

} a[10000];

bool s(const words& a,const words& b);

//==========================================
int main() {
int i,x;
string word;

//input-output with files
ifstream cin("wordin.txt");
ofstream cout("wordout.txt");

i = 0;

//reading until the end of the file
while(!cin.eof()){
cin >> word;

x = word.size();

a[i].sizes = x; //the problem is here

a[i].lexis = word;
i++;
}

}

如果有人帮助我,我将不胜感激。 :)谢谢

最佳答案

避免使用与标准库名称相同的变量,在您的情况下,将两个文件流重命名为 cincout,例如,my_cinmy_cout

如果你想读取多个 stringint 使用 std::vector 而不是数组,即代替你的 struct words 你可以使用:

vector<string> words;

然后要从文件中读取,您可以执行以下操作:

// attach stream to file
ifstream my_cin("wordin.txt");

// check if successfully opened
if (!my_cin) cerr << "Can't open input file!\n";

// read file line by line
string line;
while (getline(my_cin, line)) {

// extract every word from a line
stringstream ss(line);
string word;
while (ss >> word) {

// save word in vector
words.push_back(word);
}
}

关于c++ - 将整数添加到结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34062383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com