C++ rand 不适用于属性初始值设定项

C++ rand doesn't work with attribute initializer

本文关键字:rand 不适用 适用于 属性 C++      更新时间:2023-10-16

我是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;
}
在这段代码中,我可以说
  1. 我读到时间(0)可以改变一些结果,但没有改变什么。
  2. 是的,我想得到一个许多对象创建它的对象向量实例化时,与向量初始化其所有的MAX_OBJS元素x属性值n
  3. 我试图做一些代码来找到一个给定的元素,在这种情况下的最后一个,在一些向量(在这种情况下,相同的元素是)。我尝试了一些事情,如findfind_if,没有成功。

我需要一些建议/帮助解决我的问题,它们是

  1. 行A)打印相同的数字(随机的)MAX_OBJS次数
  2. 我不知道比B更好的比较对象的方法
  3. 行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;