C++ 打印包含数据结构的数组
C++ Printing an array containing data structures
我目前是使用C++的初学者,在尝试弄清楚如何打印数据结构数组时遇到了一些麻烦。我一直在寻找试图找到解决方案,但是,我没有成功。下面是我的代码:
#include <iostream>
#include <string>
using namespace std;
int P = 5;
int N = 5;
//Create individual
typedef struct
{
int gene[5];
int fitness;
} individual;
//Population array contains all individuals
individual population[5];
//Initialize genes and fitness level of each individual in the the
population
int initializeGenes()
{
for (int i = 0; i < P; i++) {
for (int j = 0; j < N; j++) {
population[i].gene[j] = rand() % 2;
}
population[i].fitness = 0;
}
return 0;
}
int main()
{
initializeGenes();
for (int i = 0; i < P; i++)
{
cout << population[i];
}
system("pause");
}
我想做的是打印"population"数组,我收到的错误说"没有运算符"<<"匹配这些操作数,操作数类型是 std::ostream <<个人",我不明白如何解决这个问题。
任何帮助或解决方案将不胜感激,谢谢!
问题是您正在尝试 cout 一个结构,并且C++此操作仅针对基元类型(int、char、char* 等(定义,除非您自己重载 <<运算符。
您可能想要的是输出结构中包含的基元数据类型。
例
for (int i = 0; i < 5; i++) {
cout << population[i].gene[i] << endl;
}
cout << population[i].fitness << endl;
您必须重载operator <<
并使用 ostream 和结构个体作为参数,以便您可以直接调用 cout。下面是一个片段:
ostream& operator << (ostream& os, const individual& ind)
{
os << "Gene = ";
for (int i = 0; i < 5; i++)
{
os << ind.gene[i] << " ";
}
os << "Fitness = " << ind.fitness <<'n';
return os;
}
这是实现目标的C++11
方法。正如@seccpur所强调的,最好的方法是使<<
通量运算符过载。我采取了一些自由,通过使用class
而不是struct
来稍微转换您的代码。它还将向您展示在玩类时<<
通量运算符的另一种用法。
#include <iostream>
#include <string>
#include <vector>
#include <random>
class Individual
{
public:
Individual() : fitness(0)
{
gene.reserve(5);
std::random_device random_dev;
std::mt19937 generator(random_dev());
std::uniform_int_distribution<int> distr(0, 100);
for (std::size_t i = 0; i < 5; ++i)
gene.push_back(distr(generator) % 2);
}
~Individual() {};
private:
std::vector<int> gene;
int fitness;
private:
friend std::ostream& operator<<(std::ostream& stream, const Individual& individual);
};
std::ostream& operator <<(std::ostream& stream, const Individual& individual)
{
stream << "Gene = ";
for (std::size_t i = 0; i < individual.gene.size(); ++i)
stream << individual.gene[i] << " ";
stream << "Fitness = " << individual.fitness <<'n';
return stream;
}
std::vector<Individual> population(5);
int main()
{
for (int i = 0; i < population.size(); i++)
std::cout << population[i];
return 0;
}
在上面的示例中,您必须绝对声明以下内容:
friend std::ostream& operator<<(std::ostream& stream, const Individual& individual);
运算符重载声明为Individual
class
friend
,以便访问其私有成员。
另外,请注意rand()
功能。按原样使用,它将始终生成相同的数字。您应该为数字生成器提供种子。它的用法之一可以是:
srand(time(NULL)); // Needed one time.
rand() % 2; // Can be used multiple time
相关文章:
- std::vector的包装器,使数组的结构看起来像结构的数组
- C++中的可变长度数组/数据结构
- C++:__aligned(__alignof__) 导致字符数组数据出现问题?
- 很好的语法来获取对向量/数组数据的大小引用?
- C++具有动态分配的字符数组的结构
- 复制 C 样式数组和结构
- 使用选择排序对数组数据结构进行排序,但它不起作用
- 数组数据以错误的方式遍历 Python/Matlab
- 如何填充包含指针数组的结构数组
- 访问 Arduino 中的结构数组数据
- 如何修复访问动态数组中结构中的字符串变量时"segmentation fault (core dumped)"错误
- sizeof(空结构)和sizeof(带有空数组的结构)之间的区别?
- 在能够从 Web 浏览器访问大型数组数据的同时存储它的最可行方法是什么?
- 初始化固定的 C 数组成员结构
- 对需要双精度数组和结构作为输入C++ DLL 函数的 C# 调用
- 将包含数组数据成员的结构保存到文件
- 数据包转换为具有多个动态数组的结构
- 从数组数据中缺少第一个元素的 DLL 返回C++结构
- 实现动态数组数据结构时出现"double free or corruption"错误
- 将字节数组数据映射到c#中的结构体