C++ 迭代器 it返回的内容以指针方式返回

    xiaoxiao2021-03-25  58

    #include<vector> class A { public: int* GetA(int b) { for(std::vector<int>::iterator it = _a.begin(); it != _a.end(); ++it) { if((*it) == b) return &(*it); //这里如果直接返回迭代器it会报错 所有取内容然后取地址返回不会报错 } return NULL; } void SetA(int b) { _a.push_back(b); } private: std::vector<int> _a; }; int main() { A a; a.SetA(10); a.SetA(20); int* x = a.GetA(10); std::cout << *x << std::endl; return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-39941.html

    最新回复(0)