C++ - 从Set中打印出对象

12
如果我有一个C++的set和iterator:
set<Person> personList;
set<Person>::const_iterator location;
我该如何打印出 set 的内容呢?这些内容都是 Person 对象,并且我已经为 Person 重载了运算符<<。
错误的代码在一个基本的 for 循环中:
cout << location

Netbeans提示:

proj.cpp:78: error: no match for ‘operator<<’ in ‘std::cout << location’

看起来它想要为迭代器的operator<<进行重载。

基本上,我正在处理以前存储在数组格式中的对象,但现在是在一个集合中。对于集合,cout << array[i] 的等效方法是什么?

5个回答

24
在C++11中,为什么要使用for循环而不是使用foreach循环?
#include <iostream> //for std::cout

void foo()
{
    for (Person const& person : personList)
    {
        std::cout << person << ' ';
    }
}

在 C++98/03 中,为什么要使用 for 循环而不是使用算法呢?

#include <iterator> //for std::ostream_iterator
#include <algorithm> //for std::copy
#include <iostream> //for std::cout

void foo()
{
    std::copy(
        personList.begin(),
        personList.end(),
        std::ostream_iterator(std::cout, " ")
        );
}

请注意,这适用于任何一对迭代器,不仅限于来自 std::set<t> 的迭代器。std::copy 将使用您定义的 operator<< 打印出 set 中的每个项目,使用这个单一语句。


7

您需要对迭代器进行解引用:

 std::cout << *location;

使用间接寻址或解引用运算符获取迭代器引用值的惯例是模拟指针时选择的:
 person* p = &somePerson;
 std::cout << *p;

哦,我明白了。这也是为什么箭头运算符允许我访问对象字段的原因。谢谢! - John Smith
1
我能不能不使用迭代器来打印一个集合,就像这样: for(int i=0; i<=s.size(); ++i) cout<<s[i]<<" "; 就像我们在向量的情况下所做的那样。我尝试了这个方法,但它显示“s[i]”中没有匹配的运算符。 - Sunny
1
@Sunny:不,std::set没有提供那个功能。顺便说一下,使用迭代器的方式几乎相同:for(SetType::const_iterator it=s.begin();it!=s.end();++it) cout<<*it;。如果您需要更多细节,您可能应该提出一个新问题。 - Georg Fritzsche
@GeorgFritzsche 是的,我知道Set是一个关联容器,所以我们不能做我提到的那个。 - Sunny

6

它是一个 set::iterator。

   for(it = output_set.begin(); it != output_set.end(); it++)
    {
        outstream_1 << *it << endl;
    }

2

begin()是C++ STL函数中的内置函数,用于返回指向set容器第一个元素的迭代器。

end()是C++ STL中的内置函数,用于获取一个迭代器,该迭代器指向最后一个元素的下一个位置。这个最后一个元素的下一个位置是理论上的,它并不指向任何元素,因此不能被解引用。

另外,Set中的对象是按升序或降序存储的不同值。

示例1:

set<char> myset{'a', 'c', 'g', 'z'}; 
for (auto it=myset.begin(); it != myset.end(); ++it)     
cout << ' ' << *it;

输出:a c g z

例子2:

set<string> myset{"This", "is", "confused"}; 
for (auto it=myset.begin(); it != myset.end(); ++it) 
cout << ' ' << *it;

输出:这是混淆的


0
// Online C++ compiler to run C++ program online
#include <iostream>
#include<set>
using namespace std;

struct Car
{
    int m;
    int p;
    void set(int a,int b){
        m =a,p = b;
    }
};
struct Car cT[50];
int cI=0;

bool operator < (const Car& c1,const Car& c2){
    return c1.p < c2.p;
}
ostream& operator << (ostream& o, const Car& a)
    {
        o << "Car Model: " << a.m << " price: " << a.p << endl;
        return o;
    }

void insertS(set<Car>& c){
    for(int i=0;i<cI;i++){
        c.insert(cT[i]);
    }
}

void printS(set<Car> c){
    set<Car> :: iterator i;
    for( i=c.begin();i!=c.end();i++){
        cout << *i<<endl;
    }
}

int main() {
    // Write C++ code here
    set<Car> mCs;
    cT[cI++].set(4,5);
    cT[cI++].set(34,4);
    cT[cI++].set(43,6);
    cT[cI++].set(41,15);
    insertS(mCs);
    printS(mCs);

    return 0;
}

输出将会是:::

Car Model: 34 price: 4

Car Model: 4 price: 5

Car Model: 43 price: 6

Car Model: 41 price: 15

目前的答案写得不清楚。请编辑以添加额外的细节,帮助他人理解这如何回答所提出的问题。您可以在帮助中心找到有关如何编写良好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接