C++ 打印multimap的所有元素,它们是C+中两个不同对象的一对+;?
我有两个类A和B。我创建对象,然后将它们放入多重贴图中。我想打印所有键及其相应的值。然而,我的尝试并没有像创建迭代器那样成功。我想问一下如何使用C++ 打印multimap的所有元素,它们是C+中两个不同对象的一对+;?,c++,oop,printing,key,multimap,C++,Oop,Printing,Key,Multimap,我有两个类A和B。我创建对象,然后将它们放入多重贴图中。我想打印所有键及其相应的值。然而,我的尝试并没有像创建迭代器那样成功。我想问一下如何使用equal_range()方法来实现这一点。提前谢谢 #include "pch.h" #include <iostream> #include <map> using namespace std; class A { int key; public: A(int k) { key = k;
equal_range()
方法来实现这一点。提前谢谢
#include "pch.h"
#include <iostream>
#include <map>
using namespace std;
class A {
int key;
public:
A(int k) {
key = k;
}
A(A ob) {
key = ob.key;
}
int getKey() {
return key;
}
};
class B {
string value;
public:
B(string v) {
value = v;
}
};
multimap <A, B> mp;
int main() {
mp = {
make_pair(A(1),B("Crime and punishment")),
make_pair(A(1),B("The Idiot")),
make_pair(A(1),B("Brothers' Karamazov")),
make_pair(A(2),B("Andreshko")),
make_pair(A(2),B("The Gerak family")),
make_pair(A(3),B("The name of the rose")),
make_pair(A(3),B("Baudolino"))
};
for (auto ml = mp.begin(); ml != mp.end();ml++) {
multimap<pair<int, string>, pair<int, string>>::iterator it;
}
}
#包括“pch.h”
#包括
#包括
使用名称空间std;
甲级{
int键;
公众:
A(整数k){
key=k;
}
A(产科医生){
key=ob.key;
}
int getKey(){
返回键;
}
};
B类{
字符串值;
公众:
B(第五串){
值=v;
}
};
多重映射mp;
int main(){
mp={
(A(1),B(“犯罪与惩罚”),
做一对(A(1),B(“白痴”),
制作一对(A(1),B(“卡拉马佐夫兄弟”),
(A(2),B(“安德列什科”),
制作一对(A(2),B(“杰拉克家族”),
制作一对(A(3),B(“玫瑰的名字”),
配对(A(3),B(“波多利诺”))
};
for(auto-ml=mp.begin();ml!=mp.end();ml++){
多重映射::迭代器;
}
}
您已经有了自己的循环,但让我们使用C++11远程循环
您只是缺少一个值的访问器,因此让我们假设您拥有它(getValue
),并且只访问迭代器:
for (const auto& pair : mp) {
std::cout << ml.first.getKey() << "\t" << ml.second.getValue() << std::endl;
}
到
这将提供一个真实的副本分配。但是默认的复制构造函数也很好,所以根本不用提,默认的复制构造函数已经对您很好了。您需要的一些东西:
- B类的Getter方法
- 密钥类别A的小于运算符
- 类A的复制构造函数
#include <iostream>
#include <map>
#include <string>
using namespace std;
class A {
int key;
public:
A(int k) : key(k){}
A(A const& ob) : key(ob.key) {}
A& operator=(A const& ob) = delete;
int getKey() const { return key; }
friend bool operator<(A const&left, A const&right) { return left.key < right.key; }
};
class B {
string value;
public:
B(string const& v) : value(v) {}
B(B const&) = default;
B& operator=(B const&) = delete;
string const& getValue() const { return value; }
};
multimap<A, B> mp;
int main() {
mp = {
make_pair(A(3), B("Baudolino")),
make_pair(A(1), B("Crime and punishment")),
make_pair(A(1), B("Brothers' Karamazov")),
make_pair(A(2), B("Andreshko")),
make_pair(A(1), B("The Idiot")),
make_pair(A(2), B("The Gerak family")),
make_pair(A(3), B("The name of the rose"))
};
for (auto const & item : mp) {
cout << "key:" << item.first.getKey() << " value:\"" << item.second.getValue() << "\"\n";
}
}
#包括
#包括
#包括
使用名称空间std;
甲级{
int键;
公众:
A(int k):键(k){}
A(常数&ob):key(ob.key){}
A&operator=(A const&ob)=删除;
int getKey()常量{return key;}
友人布尔算子
A(const A& ob)
#include <iostream>
#include <map>
#include <string>
using namespace std;
class A {
int key;
public:
A(int k) : key(k){}
A(A const& ob) : key(ob.key) {}
A& operator=(A const& ob) = delete;
int getKey() const { return key; }
friend bool operator<(A const&left, A const&right) { return left.key < right.key; }
};
class B {
string value;
public:
B(string const& v) : value(v) {}
B(B const&) = default;
B& operator=(B const&) = delete;
string const& getValue() const { return value; }
};
multimap<A, B> mp;
int main() {
mp = {
make_pair(A(3), B("Baudolino")),
make_pair(A(1), B("Crime and punishment")),
make_pair(A(1), B("Brothers' Karamazov")),
make_pair(A(2), B("Andreshko")),
make_pair(A(1), B("The Idiot")),
make_pair(A(2), B("The Gerak family")),
make_pair(A(3), B("The name of the rose"))
};
for (auto const & item : mp) {
cout << "key:" << item.first.getKey() << " value:\"" << item.second.getValue() << "\"\n";
}
}