如何使用适当的迭代器在 C++ 中编写 class,以便标准库容器和算法可以使用它
How to write a class in c++ with proper iterators so it can be used by standard library containers and algorithms
假设我用 c++ 写了一个 class 像这样:
class data
{
private:
int a,b,c,d;
public:
data(){
a=b=c=d=0;
}
data(int aa,int bb, int cc, int dd)
{
a=aa,b=bb,c=cc,d=dd;
}
int get_b(){return b;}
int get_a(){return a;}
};
现在我知道想学习如何为此编写运算符和迭代器 class 以便它可以与标准库容器一起使用
赞:set<string> x;
这是可以的。我想将此 data class 用作 set<data> x;
并希望使用像 set<data>::iterator it;
这样的迭代器
我已经尝试 google 完成该过程,但找不到真正解释该过程的示例实现。
如果您想在 std::set
、std::map
、std::unordered_map
、std::unordered_set
中使用它,您需要实现 "weak less than operator"
bool operator<(const data& other_data) {
return ....;
}
对于 std::vector
、std::deque
、std::list 等其他容器,数据正常。
Like: set x; this is possible. I want to use this data class
as set x; and want to use iterators like set::iterator it;
我相信您想将 class 用作适合标准容器(vector/set 等)的数据。
对于序列容器(向量)
int main()
{
data d1(1, 2);
data d2(3, 4);
vector<data> vec{d1, d2}; //initialize your container with your "data" type objects.
for(auto iter = vec.begin(), iter != vec.end();iter++)
cout<<*iter; //for this you need to implement "operator<<" in your data class.
return 0;
}
对于关联容器(集)
int main()
{
data d1(1, 2);
data d2(3, 4);
//you need to impelment "< or >" relational operator in you data class.
set<data> st{d1, d2}; //initialize your container with your "data" type objects.
for(auto iter = vec.begin(), iter != vec.end();iter++)
cout<<*iter; //for this you need to implement "operator<<" in your data class.
return 0;
}
将此添加到您的 class 定义中。
friend ostream& operator<<(ostream& os, const base& obj)
{
return (os<<obj.memVariable);
}
假设我用 c++ 写了一个 class 像这样:
class data
{
private:
int a,b,c,d;
public:
data(){
a=b=c=d=0;
}
data(int aa,int bb, int cc, int dd)
{
a=aa,b=bb,c=cc,d=dd;
}
int get_b(){return b;}
int get_a(){return a;}
};
现在我知道想学习如何为此编写运算符和迭代器 class 以便它可以与标准库容器一起使用
赞:set<string> x;
这是可以的。我想将此 data class 用作 set<data> x;
并希望使用像 set<data>::iterator it;
我已经尝试 google 完成该过程,但找不到真正解释该过程的示例实现。
如果您想在 std::set
、std::map
、std::unordered_map
、std::unordered_set
中使用它,您需要实现 "weak less than operator"
bool operator<(const data& other_data) {
return ....;
}
对于 std::vector
、std::deque
、std::list 等其他容器,数据正常。
Like: set x; this is possible. I want to use this data class as set x; and want to use iterators like set::iterator it;
我相信您想将 class 用作适合标准容器(vector/set 等)的数据。
对于序列容器(向量)
int main()
{
data d1(1, 2);
data d2(3, 4);
vector<data> vec{d1, d2}; //initialize your container with your "data" type objects.
for(auto iter = vec.begin(), iter != vec.end();iter++)
cout<<*iter; //for this you need to implement "operator<<" in your data class.
return 0;
}
对于关联容器(集)
int main()
{
data d1(1, 2);
data d2(3, 4);
//you need to impelment "< or >" relational operator in you data class.
set<data> st{d1, d2}; //initialize your container with your "data" type objects.
for(auto iter = vec.begin(), iter != vec.end();iter++)
cout<<*iter; //for this you need to implement "operator<<" in your data class.
return 0;
}
将此添加到您的 class 定义中。
friend ostream& operator<<(ostream& os, const base& obj)
{
return (os<<obj.memVariable);
}