C++ 如何在类中轻松创建向量或数组以及如何访问它们
例如,C++ 如何在类中轻松创建向量或数组以及如何访问它们,c++,class,constructor,C++,Class,Constructor,例如,向量A(5)可以返回5个元素的向量。在我们自己构建的类中,是否可以做类似的事情 例如,我有一个叫做水果的课程。我应该编写什么构造函数,使产生一个(5)可以给我返回5个苹果。这意味着apples是此类的私有成员,应该是大小为5的数组或向量。我没有编写苹果的详细信息的原因是我不知道如何在成员中正确地声明它并创建它的构造函数 最后,我创建了水果A(5),我只能通过在类中创建get()函数来访问此数组的成员,还是有更方便的方法 如果你能给我一些建议,我真的很感激。谢谢 最简单的策略是让类包含一个容
向量A(5)代码>可以返回5个元素的向量。在我们自己构建的类中,是否可以做类似的事情
例如,我有一个叫做水果的课程。我应该编写什么构造函数,使产生一个(5)
可以给我返回5个苹果
。这意味着apples
是此类的私有成员,应该是大小为5的数组或向量。我没有编写苹果的详细信息的原因是我不知道如何在成员中正确地声明它并创建它的构造函数
最后,我创建了水果A(5)
,我只能通过在类中创建get()
函数来访问此数组的成员,还是有更方便的方法
如果你能给我一些建议,我真的很感激。谢谢 最简单的策略是让类包含一个容器。如果您不想为其写入访问器,请将其设置为公共成员:
class Fruit
{
public:
Fruit() { }
Fruit(int asize) : basket_(asize) { }
vector<apple> basket_ ;
} ;
最简单的策略是让类包含一个容器。如果您不想为其写入访问器,请将其设置为公共成员:
class Fruit
{
public:
Fruit() { }
Fruit(int asize) : basket_(asize) { }
vector<apple> basket_ ;
} ;
您希望将苹果
存储在向量
或数组
中。然后,您需要覆盖[]操作符
,以访问所需的内容。比如:
class Fruit
{
private:
vector<apple> apple_storage;
public:
int& operator[](int j) {return apple_storage[j];}
};
类水果
{
私人:
矢量苹果存储;
公众:
int&运算符[](int j){return apple_storage[j];}
};
然后,您可以轻松地使用A[3]
访问apple\u存储
您想要将苹果
存储在向量
或数组中。然后,您需要覆盖[]操作符
,以访问所需的内容。比如:
class Fruit
{
private:
vector<apple> apple_storage;
public:
int& operator[](int j) {return apple_storage[j];}
};
类水果
{
私人:
矢量苹果存储;
公众:
int&运算符[](int j){return apple_storage[j];}
};
然后您可以轻松地使用A[3]
访问您的apple\u存储
而无需了解apple
的详细信息,并且假设它们有一个默认构造函数,下面是一个简单的方法
class Fruits {
public:
typedef std::vector<apple> AppleArray;
typedef AppleArray::iterator AppleIterator;
typedef AppleArray::const_iterator AppleConstIterator;
Fruits(int appleCount)
: mApples(appleCount)
{
}
~Fruits()
{
}
apple& getAppleAt(size_t index)
{
return mApples.at(index);
}
const apple& getAppleAt(size_t index) const
{
return mApples.at(index);
}
AppleIterator getAppleBegin()
{
return mApples.begin();
}
AppleIterator getAppleEnd()
{
return mApples.end();
}
AppleConstIterator getAppleBegin() const
{
return mApples.begin();
}
AppleConstIterator getAppleEnd() const
{
return mApples.end();
}
size_t getAppleCount() const
{
return mApples.size();
}
private:
std::vector<apple> mApples;
};
类水果{
公众:
typedef std::vector applarray;
typedef applarray::迭代器appleierator;
typedef applarray::const_迭代器appleconstitrator;
水果(国际苹果计数)
:mApples(应用计数)
{
}
~Fruits()
{
}
苹果和getAppleAt(大小索引)
{
返回mApples.at(索引);
}
常量苹果和getAppleAt(大小索引)常量
{
返回mApples.at(索引);
}
AppleIterator getAppleBegin()
{
返回mApples.begin();
}
AppleIterator getAppleEnd()
{
返回mApples.end();
}
appleconstitrator getAppleBegin()常量
{
返回mApples.begin();
}
AppleConstitator getAppleEnd()常量
{
返回mApples.end();
}
大小\u t getAppleCount()常量
{
返回mApples.size();
}
私人:
std::向量映射;
};
我假设这不是生产代码,它是纯粹学习C++的。如果你要在现实生活中设计一个类似的东西,你需要考虑<代码>苹果<代码>是一个<代码>水果<代码>,而<>代码>水果<代码>更像是一个集合,一个或多个水果,一个是代码>苹果< /Cord>。在这种情况下,您的水果
更像std::vector水果
然后使用一个循环来初始化水果列表中想要的苹果
的数量没有太多的细节进入苹果
,假设它们有一个默认构造函数,这里有一个简单的方法
class Fruits {
public:
typedef std::vector<apple> AppleArray;
typedef AppleArray::iterator AppleIterator;
typedef AppleArray::const_iterator AppleConstIterator;
Fruits(int appleCount)
: mApples(appleCount)
{
}
~Fruits()
{
}
apple& getAppleAt(size_t index)
{
return mApples.at(index);
}
const apple& getAppleAt(size_t index) const
{
return mApples.at(index);
}
AppleIterator getAppleBegin()
{
return mApples.begin();
}
AppleIterator getAppleEnd()
{
return mApples.end();
}
AppleConstIterator getAppleBegin() const
{
return mApples.begin();
}
AppleConstIterator getAppleEnd() const
{
return mApples.end();
}
size_t getAppleCount() const
{
return mApples.size();
}
private:
std::vector<apple> mApples;
};
类水果{
公众:
typedef std::vector applarray;
typedef applarray::迭代器appleierator;
typedef applarray::const_迭代器appleconstitrator;
水果(国际苹果计数)
:mApples(应用计数)
{
}
~Fruits()
{
}
苹果和getAppleAt(大小索引)
{
返回mApples.at(索引);
}
常量苹果和getAppleAt(大小索引)常量
{
返回mApples.at(索引);
}
AppleIterator getAppleBegin()
{
返回mApples.begin();
}
AppleIterator getAppleEnd()
{
返回mApples.end();
}
appleconstitrator getAppleBegin()常量
{
返回mApples.begin();
}
AppleConstitator getAppleEnd()常量
{
返回mApples.end();
}
大小\u t getAppleCount()常量
{
返回mApples.size();
}
私人:
std::向量映射;
};
我假设这不是生产代码,它是纯粹学习C++的。如果你要在现实生活中设计一个类似的东西,你需要考虑<代码>苹果<代码>是一个<代码>水果<代码>,而<>代码>水果<代码>更像是一个集合,一个或多个水果,一个是代码>苹果< /Cord>。在这种情况下,您的水果
更像std::vector水果
然后你用一个循环来初始化水果列表中你想要的苹果
的数量
该死的,操作符覆盖,我怎么会忘记呢!伙计,非常感谢你。这很有帮助!该死,操作员超控,我怎么会忘记!伙计,非常感谢你。这很有帮助@Cancan:我们已经将C
定义为类成员,因此初始化器列表中的C(size)
表示“通过将size
作为参数传递给构造函数来初始化C
”。在这里不要担心继承问题;正如Vite所说,从标准容器继承是个坏主意。让我把它重命名为一个小C->basket_u@Cancan:你是在问如何创建一个对象并访问它的公共成员吗?以通常的方式创建对象,Fruit A(5)
或其他什么,并以通常的方式访问其公共成员,A.apple[3]
。你的入门书没教你吗?@ViteFalcon并不是所有的继承都是为po准备的