使用C 中的向量创建整数集类别
Creating an Integer set class using vectors in C++
我试图编写C 中整数类的代码,并且类对象(集合)应作为向量表示:
:创建一个名为IntegerSet的类。类Integerset类的每个对象都可以将整数在范围0中通过集合的大小(或100个指定)。在内部,一个集合表示为一个和零的向量。如果我不包含在集合中,则索引I处的元素设置为零,或者否则为1。
- 提供两个班级的构造函数。第一个构造函数采用集合的大小,并相应地初始化向量。最初,所有值都是零。默认构造函数初始化了100个元素的向量。
我对如何将类对象初始化为向量并操纵它感到困惑,因为我使用的方法给出了以下错误:没有匹配'operator []或什么也没输出。
我的代码如下:
#include<iostream>
#include<vector>
using namespace std;
class IntSet: public vector<int>
{
public:
//constructors: first one takes the size of a vector and initializes it accordingly,
//second one initializes the vector to be of size 100
IntSet(int size);
IntSet();
// intersection - that creates a third set that contains the common elements between two sets.
Intersection(IntSet& set2);
//friend ostream& operator<<(ostream&, const )
};
IntSet::IntSet(int size)
{
vector<int> set (size);
for(int i=0;i<size;i++)
set[i] =0;
}
IntSet::IntSet()
{
vector<int> set (100);
}
/*
vector<int> IntSet::Intersection(IntSet& set2)
{
vector<int> setOFcommon;
for
}
*/
int main()
{
IntSet object(2);
object[1]=9;
object[2]=7;
for(int x: object)
cout<<x;
}
这就是如何做
class IntSet
{
public:
IntSet(int size);
private:
vector<int> set;
};
IntSet::IntSet(int size) : set(size)
{
for (int i = 0; i < size; i++)
set[i] = 0;
}
您将set
向量的声明放在类中,而不是在构造函数中。在构造函数中,它只是局部变量,设置对您的课程根本没有影响。
也不从std :: vector继承。您的类具有向量,它不是一种向量。将继承用于一种关系。
相关文章:
- 如何从一组具有从左到右优先级的整数值创建有序整数键?
- 创建整数的 2D 数组,该数组将使用两个函数用随机数填充矩阵.我做错了什么?
- 如何创建128位整数文本
- 我需要帮助创建一个评分系统,但它一直给我一个错误,注释掉的整数是给我带来麻烦的部分
- 创建<int>对整数数组指针的矢量引用 (C++)
- 当 map 是一个整数数组并且由 operator[] 创建时,它是否初始化其映射类型
- 编写所需的代码以创建动态一维整数数组
- 如何使用 QFile、c++ 从文件创建整数数组
- 如何在给定任意数量的整数的情况下创建一个唯一键?并使用该键存储,然后从地图中查找
- 我不知道如何创建一个以数字/字母顺序打印出我的整数或字符串的程序
- bad_alloc::'标量删除析构函数'(无符号整数)当我尝试创建矢量 470MB 大小时
- C MPI创建并发送具有字段char [16]和整数的结构数组
- 从其他两个文件中创建一个分类整数的文件
- 使用C 中的向量创建整数集类别
- 在数组C++中创建所有可能的非唯一整数组合
- 如何从 3 个有序整数创建哈希
- 使用整数模板参数创建编译时双精度
- 如何基于许多不同的整数创建多种颜色
- 有没有一种方法可以为非整数创建枚举数据类型
- 如何使用两个键作为整数创建 2D 向量