访问一组指针
Accessing a set of pointers
下面是我的代码:
class obj140{
public:
int x;
explicit obj140(int y):x(y){ }
bool operator<(const obj140& rhs) const{
return x < rhs.x;
}
};
int main() {
obj140 * wtf = new obj140[5] {obj140(1),obj140(1),obj140(3),obj140(4),obj140(5)};
std::set<obj140> orm(wtf,wtf+5);
}
这可能吗?比如把指针复制到集合?我没有错误,但我不知道如何访问它。
如何打印出orm
集合的值?
我稍微修改了你的代码,使发生的事情更容易看到,并作为一个例子,一种方式来查看项目存储在set
。
class obj140
{
public:
int x;
explicit obj140(int y) :x(y)
{
}
bool operator<(const obj140& rhs) const
{
return x < rhs.x;
}
void print() const
{
std::cout << x << std::endl;
}
};
int main()
{
obj140 * wtf = new obj140[5]
{ obj140(1), obj140(1), obj140(3), obj140(4), obj140(5) };
std::set<obj140> orm(wtf, wtf + 5);
for (auto it = orm.begin(); it != orm.end(); ++it)
{
it->print();
}
delete[] wtf; //edit. Forgot to clean up the pointer.
return 0;
}
输出:1
3
4
5
你正在做的工作和加载set
。由于set
s只存储唯一值(并对它们进行排序,如果需要的话,这可以进行非常快速的排序),因此丢弃了obj140(1)
的第二次添加。
相关文章:
- 在 c++ 中拥有一组结构的正确方法是什么?
- 有哪些有效的方法可以消除一组 100 万个字符串>重复数据?
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 添加到数组指针
- 一组值的零开销下标运算符
- C++语法差异:二维和一维数组(指针算术)
- 在删除之前更新一组元素指针中的元素
- 是否可以为CPP中的对象创建一组指针
- 在一组指针上迭代
- Visual Studio 2015:是一组允许的常量指针
- 正确删除一组指针
- 如何用自定义比较表示一组指针,但保持原始原始指针的重复比较
- 我应该使用 std::set 还是 std::unordered_set 作为一组指针
- 如何在C++中从一组对象指针打印
- c++指针从一组数组
- c++:在一组类指针上使用std::find
- 存储一组指针会导致未定义的行为吗
- C++保留了一组指向模板对象的指针,这些指针都派生自一个非模板类
- 访问一组指针
- 混合程序(asm+cpp):发送和接收一个数组指针