C++ rand 不适用于属性初始值设定项
C++ rand doesn't work with attribute initializer
我是c++的新手,有一些C的经验,为了学习它,我让自己在做作业的时候测试了一些东西。现在,我有
#define MAX_OBJS 4
using namespace std;
class Object {
public:
int x, rand;
Object(int y) {
x = y;
rand = rand() % 5;
};
};
class Many {
public:
vector<Object> obj_list;
Many(int n): obj_list (MAX_OBJS, n) {}
};
int main() {
srand(time(NULL));
Many many(42);
cout << "Example: looking for " << many.obj_list.back().rand "n";
vector<Object>::iterator j;
Object t = many.obj_list.back();
for (j = many.obj_list.begin(); j != many.obj_list.end(); j++) {
/*A*/ cout << j->rand << "n";
/*B*/ if (&(*j) == &t)
/*C*/ cout << "Found!" << "n";
}
return EXIT_SUCCESS;
}
在这段代码中,我可以说
- 我读到时间(0)可以改变一些结果,但没有改变什么。
- 是的,我想得到一个许多对象创建它的对象向量实例化时,与向量初始化其所有的MAX_OBJS元素x属性值n。
- 我试图做一些代码来找到一个给定的元素,在这种情况下的最后一个,在一些向量(在这种情况下,相同的元素是)。我尝试了一些事情,如find和find_if,没有成功。
我需要一些建议/帮助解决我的问题,它们是
- 行A)打印相同的数字(随机的)MAX_OBJS次数
- 我不知道比B更好的比较对象的方法
- 行C)从不输出"Found! "
find不起作用,即使有@Nawaz的建议。编译器说:
/usr/include/c++/4.5/bits/stl_algorithm .h: In function ' RandomAccessIterator std::_find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator>, _Tp = Object] ':
/usr/include/c++/4.5/bits/stl_algo.h: 4109:45: instantiated from ' _IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator>, _Tp = Object] '
./src/Many.cpp:48:74: instantiated from here
/usr/include/c++/4.5/bits/stl_algorithm .h:158:4: error: no match for ' operator== ' in ' _first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Object*, _Container = std::vector, __gnu_cxx::__normal_iterator<_Container>::reference = Object&= = __val '
/usr/include/c++/4.5/bits/stl_algo.h: 4109:45: instantiated from ' _IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator>, _Tp = Object] '
./src/Many.cpp:48:74: instantiated from here
/usr/include/c++/4.5/bits/stl_algorithm .h: 62:4: error: no match for ' operator== ' in ' _first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Object*, _Container = std::vector, __gnu_cxx::__normal_iterator<= = __val '
/usr/include/c++/4.5/bits/stl_algorithm .h: 160:4: error: no match for ' operator== ' in ' _first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Object*, _Container = std::vector, __gnu_cxx::__normal_iterator<= = __val '
/usr/include/c++/4.5/bits/stl_algorithm .h:170:4: error: no match for ' operator== ' in ' _first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Object*, _Container = std::vector, __gnu_cxx::__normal_iterator<= = __val '
/usr/include/c++/4.5/bits/stl_algorithm .h:178:4: error: no match for ' operator== ' in ' _first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Object*, _Container = std::vector, __gnu_cxx::__normal_iterator<= = __val '
/usr/include/c++/4.5/bits/stl_algorithm .h: 82:4: error: no match for ' operator== ' in ' _first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Object*, _Container = std::vector, __gnu_cxx::__normal_iterator<= = __val '
/usr/include/c++/4.5/bits/stl_algorithm .h:186:4: error: no match for ' operator== ' in ' _first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = Object*, _Container = std::vector, __gnu_cxx::__normal_iterator<= = __val '
谢谢!
if (&(*j) == &t)
您正在比较上面if
条件中对象的地址。但是下面这行是原始对象的一个副本:
Object t = many.obj_list.back();
copy表示t
不是列表中的同一对象。因此,您的程序永远不会打印Found!
。
我认为你应该这样写:
Object & t = many.obj_list.back();
// ^ note this!
存储对象的引用。它不复制obj_list
中的原始对象。
顺便说一下,为什么不用<algorithm>
头文件中的std::find
?
#include <algorithm>
std::vector<Object>::iterator it = std::find(many.obj_list.begin(), many.obj_list.end(), t)
if ( it != many.obj_list.end())
std::cout << "Found!"<< std::endl;
- 为什么我不能将 rand() 与数组的大小一起使用?
- 为什么 rand 不在我的代码中生成随机数?
- 在 c++ 中,rand() 在执行有调试和不带调试的程序时生成不同的结果
- OpenGL/Glew C++纹理不适用
- 为什么 someNumber = rand() & 100 + 1;不产生错误?
- 十进制到二进制的实现不能完全适用于我大学的检查器。问题或提示可能是什么
- 保证复制省略不应该适用吗?
- 当我在 windows7 中安装程序时,我指定的字体大小不适用
- 使用程序集嵌入数据时"Undefined reference"错误,使用适用于窗口的 mingw-w64 编译(COFF 而不是 ELF)
- accelerator.cu(8): 错误:属性"managed"在这里不适用?
- typedef X=<T>T::UserType1,但如果不适用,typedef X<T>=UserType2
- 用C++编写一个程序,对一个简单的序列求和:1/N + 2/N-1 + 3/N-2+ ...不适用
- 为什么stdlib中的rand不遵循大数定律
- SFINAE在这里不适用吗?
- 随机数生成器 (rand) 不是随机的?
- 当是复制和交换习语不适用
- rand() 不创建随机数
- Rand()不涉及时间的种子
- 在函数实参中使用模板形参不适用gcc4.8
- 随机数(rand)不能输出未知字符