在multimap<int, Person> & personGroup(vector& v)返回值是引用会报错(下面是报错时在xtree文件的代码):
if (!_Rootnode->_Isnil) { // copy or move a node, then any subtrees
bool_constant<is_same_v<key_type, value_type>> _Is_set;
_Nodeptr _Pnode = _Copy_or_move(_Rootnode->_Myval, _Movefl, _Is_set);
_Pnode->_Parent = _Wherenode;
_Pnode->_Color = _Rootnode->_Color;
if (_Newroot->_Isnil) {
_Newroot = _Pnode; // memorize new root
}
而multimap<int, Person> personGroup(vector& v)这种不返回引用就能正常运行这是为什么。
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<math.h>
#include"person.h"
using namespace std;
void printPerson(multimap<int, Person>& mp)
{
cout << mp.begin()->first << endl;
for (multimap<int, Person>::iterator it = mp.begin(); it != mp.end(); it++)
{
if (it->first == 1)
{
cout << "员工" << it->second.m_Name
<< "的工资为 " << it->second.m_Salary << " 元,"
<< "在策划部" << endl;
}
else if (it->first == 2)
{
cout << "员工" << it->second.m_Name
<< "的工资为 " << it->second.m_Salary << " 元,"
<< "在美术部" << endl;
}
else if (it->first == 3)
{
cout << "员工" << it->second.m_Name
<< "的工资为 " << it->second.m_Salary << " 元,"
<< "在研发部" << endl;
}
}
}
multimap<int, Person> & personGroup(vector<Person>& v)
{
multimap<int, Person> mp;
if (!v.empty())
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
{
if ((*it).m_DId == 1)
{
mp.insert(pair<int, Person>((*it).m_DId, (*it)));
//mp.insert(make_pair(1, *it));
}
else if ((*it).m_DId == 2)
{
mp.insert(pair<int, Person>((*it).m_DId, (*it)));
/*mp.insert(make_pair(2, *it));*/
}
else if ((*it).m_DId == 3)
{
mp.insert(pair<int, Person>((*it).m_DId, (*it)));
//mp.insert(make_pair(3, *it));
}
}
}
cout << "vector";
return mp;
}
int main()
{
vector<Person> v;
v.push_back(Person("张三", 22, 1));
v.push_back(Person("李四", 29, 3));
v.push_back(Person("王五", 20, 3));
v.push_back(Person("钱大", 36, 2));
v.push_back(Person("赵二", 24, 1));
v.push_back(Person("陈六", 25, 2));
v.push_back(Person("孙七", 22, 2));
v.push_back(Person("郑八", 21, 1));
v.push_back(Person("魏九", 22, 1));
v.push_back(Person("黄十", 26, 3));
cout << v.size() << endl;
cout << "nihhhhhhhhhhhhhhhh" << endl;
multimap<int, Person> mp = personGroup(v);
printPerson(mp);
//pringPerson(personGroup(v));
return 0;
}