C++ C++;多地图打印
我试着说出一个名字,然后列出这个人修过的课程。这是我目前的代码:C++ C++;多地图打印,c++,multimap,C++,Multimap,我试着说出一个名字,然后列出这个人修过的课程。这是我目前的代码: #include <iostream> #include <string> #include <map> #include <list> using namespace std; int main() { multimap<string,string> students; students.insert(make_pair("john&quo
#include <iostream>
#include <string>
#include <map>
#include <list>
using namespace std;
int main()
{
multimap<string,string> students;
students.insert(make_pair("john","cs1"));
students.insert(make_pair("john","cs2"));
for (auto itr = students.begin(); itr != students.end(); ++itr)
{
cout << itr->first << '\t' << itr->second << '\n';
}
}
如果我想让它打印出第一个值,然后打印出所有第二个值,我该怎么做?我需要使用列表作为第二个值还是什么?或者,仅使用多重贴图就可以实现这一点?例如:
John CS1 CS2
您可以使用
equal_range
实现您想要的功能,请参见:
std::string current_key = "";
for (auto itr = students.begin(); itr != students.end(); ++itr)
{
if(current_key == itr->first) {
continue;
} else {
current_key = itr->first;
}
const auto result = students.equal_range(itr->first);
cout << itr->first << ": ";
for(auto it = result.first; it != result.second; ++it) {
cout << it->second << " ";
}
cout << endl;
}
std::string current_key=”“;
对于(自动itr=students.begin();itr!=students.end();++itr)
{
如果(当前_键==itr->first){
继续;
}否则{
当前_键=itr->first;
}
const auto result=students.equal_range(itr->first);
cout first您可以使用equal_range
实现您想要的功能,请参见:
std::string current_key = "";
for (auto itr = students.begin(); itr != students.end(); ++itr)
{
if(current_key == itr->first) {
continue;
} else {
current_key = itr->first;
}
const auto result = students.equal_range(itr->first);
cout << itr->first << ": ";
for(auto it = result.first; it != result.second; ++it) {
cout << it->second << " ";
}
cout << endl;
}
std::string current_key=”“;
对于(自动itr=students.begin();itr!=students.end();++itr)
{
如果(当前_键==itr->first){
继续;
}否则{
当前_键=itr->first;
}
const auto result=students.equal_range(itr->first);
cout first您最终可能会创建一个类来模拟一个学生,同时,您可以迭代多重映射
容器,检查当前键是否与前面的键相同:
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <string>
using students_t = std::multimap<std::string, std::string>;
void print_students_list(students_t const& students)
{
// Start printing the name of the first student and the first course (the first key-value
// pair) if the container is not empty.
auto student{ students.cbegin() };
if ( student == students.cend() )
return;
std::cout << std::setw(12) << std::left << student->first
<< std::setw(8) << student->second;
// Then iterate over the rest of the container printing the name (after a newline)
// only if it's different from the previous one.
for ( auto prev{ student++ }; student != students.cend(); prev = student, ++student )
{
if ( student->first != prev->first )
std::cout << '\n' << std::setw(12) << student->first;
std::cout << std::setw(8) << student->second;
}
std::cout << '\n';
}
int main()
{
students_t students {
{"john", "cs1"},
{"john", "cs2"},
{"Karen", "cs2"},
{"Karen", "nc1"},
{"Karen", "nc2"},
{"Bob", "nc1"}
};
print_students_list(students);
}
#包括
#包括
#包括
#包括
#包括
使用students\u t=std::multimap;
无效打印学生名单(学生人数和学生人数)
{
//开始打印第一个学生和第一门课程的名称(第一个键值
//配对)如果容器不是空的。
自动学生{students.cbegin()};
if(student==students.cend())
返回;
std::cout您最终可能会创建一个类
来模拟一个学生,同时,您可以迭代多映射
容器,检查当前键是否与前面的相同:
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <string>
using students_t = std::multimap<std::string, std::string>;
void print_students_list(students_t const& students)
{
// Start printing the name of the first student and the first course (the first key-value
// pair) if the container is not empty.
auto student{ students.cbegin() };
if ( student == students.cend() )
return;
std::cout << std::setw(12) << std::left << student->first
<< std::setw(8) << student->second;
// Then iterate over the rest of the container printing the name (after a newline)
// only if it's different from the previous one.
for ( auto prev{ student++ }; student != students.cend(); prev = student, ++student )
{
if ( student->first != prev->first )
std::cout << '\n' << std::setw(12) << student->first;
std::cout << std::setw(8) << student->second;
}
std::cout << '\n';
}
int main()
{
students_t students {
{"john", "cs1"},
{"john", "cs2"},
{"Karen", "cs2"},
{"Karen", "nc1"},
{"Karen", "nc2"},
{"Bob", "nc1"}
};
print_students_list(students);
}
#包括
#包括
#包括
#包括
#包括
使用students\u t=std::multimap;
无效打印学生名单(学生人数和学生人数)
{
//开始打印第一个学生和第一门课程的名称(第一个键值
//配对)如果容器不是空的。
自动学生{students.cbegin()};
if(student==students.cend())
返回;
std::cout“打印出所有第二个值”是什么意思?打印出所有第一个=john的第二个值?我希望它打印出映射的第一个值,即john。接下来,我希望它列出链接到john的每个值。就像john CS1 cs2“打印出所有第二个值”是什么意思?打印出所有第一个=john的第二个值?我希望它打印映射的第一个值,即John。接下来,我希望它列出链接到John的每个值。就像John CS1 CS2pretty尼斯解决方案一样。使用当前_键解决它的原因是multimap包含一个键值对的排序列表。是的,我不确定这一点,但这是std::multimap的要求
所以它应该可以工作。这正是我所需要的!非常感谢。您能解释一下当前的\u键用于什么以及equal\u range用于什么吗?不客气!解释:equal\u range
返回给定键的所有元素-这几乎正是您所需要的。但我们希望每个键只使用一次。这是current_key
:在迭代所有键时,给定的键将至少出现一次,但我们希望只迭代给定的键一次-因此我们只对给定键的第一次出现执行打印。如果current_key==itr->first,则表示我们已经覆盖了该键,因此我们继续并获得下一个键。非常好解决方案。使用current_key解决的原因是multimap包含一个键值对的排序列表。是的,我不确定这一点,但这是std::multimap
的要求,所以它应该可以工作。这正是我需要的!非常感谢。你能解释一下当前_key用于什么以及相等的_范围用于什么吗?你知道吗欢迎使用!说明:equal\u range
返回给定键的所有元素-这几乎正是您所需要的。但我们希望对每个键只使用一次。这就是当前键的目的:current\u key
:在对所有键进行迭代时,给定键将至少出现一次,但我们只希望对给定键进行一次迭代-嗯ce我们只对给定密钥的第一次出现执行打印。如果当前_key==itr->first,则表示我们已经覆盖了该密钥,因此我们继续并获取下一个密钥。