C++ 访问 class 中容器的 begin()/end() 方法
C++ Accessing to begin()/end() methods of container inside a class
我想在 class 中访问 Container 的 begin() 和 end() 方法,而不会在 const_iterator 中引起迭代器转换问题。所以我为 return 容器创建了一个 get 方法并访问它:
#include <iostream>
#include <vector>
class SpecialList {
public:
std::vector<int> getVett(void) const { return vettore; }
void getFull(void) {
std::vector<int>::iterator it1;
for (size_t i = 0; i < 10; ++i)
vettore.push_back(i);
}
void print(void) {
std::vector<int>::iterator it1;
std::cout << std::endl;
for (it1 = vettore.begin(); it1 != vettore.end(); ++it1)
std::cout << " " << *it1;
std::cout << std::endl;
}
private:
char some_data;
std::vector<int> vettore;
};
int main(void) {
std::cout << "Some output" << std::endl;
SpecialList listspec;
listspec.getFull();
listspec.print();
std::vector<int> pVet = listspec.getVett();
std::cout << "Size = " << pVet.size() << std::endl;
std::cout << "pVet[1] = " << pVet[1] << std::endl;
std::vector<int>::iterator it2;
std::cout << std::endl;
for (it2 = listspec.getVett().begin(); it2 != listspec.getVett().end(); ++it2)
std::cout << " " << *it2;
std::cout << std::endl << "pVet[1] = " << pVet[1] << std::endl;
return 0;
}
该代码从编译器的角度来看是有效的,但它给出了错误的输出:
Some output
0 1 2 3 4 5 6 7 8 9
Size = 10
pVet[1] = 1
0 0 2 3 4 5 6 7 8 9
pVet[1] = 1
为什么它不能正确读取打印 0 而不是 1 的矢量?这是通过迭代器访问 class 内的容器的好方法吗?
谢谢。
您的函数 std::vector<int> getVett(void) const { return vettore; }
创建了向量的副本 vettore
。您必须 return 对矢量的引用。因此,您在 for
循环中有不明确的行为。像这样更改您的功能:
const std::vector<int>& getVett(void) const { return vettore; }
因为你的函数是 const
并且你的 return 引用是 const
你必须使用 const_iterator
, cbegin
和 cend
你的 for
循环。
std::vector<int>::const_iterator it2;
for (it2 = listspec.getVett().cbegin(); it2 != listspec.getVett().cend(); ++it2)
std::cout << " " << *it2;
注意:您可以使用 auto
代替 const_iterator
:
for (auto it2 = listspec.getVett().cbegin(); it2 != listspec.getVett().cend(); ++it2)
std::cout << " " << *it2;
你也能放弃吗const
:
std::vector<int>& getVett(void) { return vettore; }
std::vector<int>::iterator it2;
for (it2 = listspec.getVett().begin(); it2 != listspec.getVett().end(); ++it2)
std::cout << " " << *it2;
我想在 class 中访问 Container 的 begin() 和 end() 方法,而不会在 const_iterator 中引起迭代器转换问题。所以我为 return 容器创建了一个 get 方法并访问它:
#include <iostream>
#include <vector>
class SpecialList {
public:
std::vector<int> getVett(void) const { return vettore; }
void getFull(void) {
std::vector<int>::iterator it1;
for (size_t i = 0; i < 10; ++i)
vettore.push_back(i);
}
void print(void) {
std::vector<int>::iterator it1;
std::cout << std::endl;
for (it1 = vettore.begin(); it1 != vettore.end(); ++it1)
std::cout << " " << *it1;
std::cout << std::endl;
}
private:
char some_data;
std::vector<int> vettore;
};
int main(void) {
std::cout << "Some output" << std::endl;
SpecialList listspec;
listspec.getFull();
listspec.print();
std::vector<int> pVet = listspec.getVett();
std::cout << "Size = " << pVet.size() << std::endl;
std::cout << "pVet[1] = " << pVet[1] << std::endl;
std::vector<int>::iterator it2;
std::cout << std::endl;
for (it2 = listspec.getVett().begin(); it2 != listspec.getVett().end(); ++it2)
std::cout << " " << *it2;
std::cout << std::endl << "pVet[1] = " << pVet[1] << std::endl;
return 0;
}
该代码从编译器的角度来看是有效的,但它给出了错误的输出:
Some output
0 1 2 3 4 5 6 7 8 9
Size = 10
pVet[1] = 1
0 0 2 3 4 5 6 7 8 9
pVet[1] = 1
为什么它不能正确读取打印 0 而不是 1 的矢量?这是通过迭代器访问 class 内的容器的好方法吗?
谢谢。
您的函数 std::vector<int> getVett(void) const { return vettore; }
创建了向量的副本 vettore
。您必须 return 对矢量的引用。因此,您在 for
循环中有不明确的行为。像这样更改您的功能:
const std::vector<int>& getVett(void) const { return vettore; }
因为你的函数是 const
并且你的 return 引用是 const
你必须使用 const_iterator
, cbegin
和 cend
你的 for
循环。
std::vector<int>::const_iterator it2;
for (it2 = listspec.getVett().cbegin(); it2 != listspec.getVett().cend(); ++it2)
std::cout << " " << *it2;
注意:您可以使用 auto
代替 const_iterator
:
for (auto it2 = listspec.getVett().cbegin(); it2 != listspec.getVett().cend(); ++it2)
std::cout << " " << *it2;
你也能放弃吗const
:
std::vector<int>& getVett(void) { return vettore; }
std::vector<int>::iterator it2;
for (it2 = listspec.getVett().begin(); it2 != listspec.getVett().end(); ++it2)
std::cout << " " << *it2;