<分区>
我想我可以像这样创建和填充 C++ 映射:
39 int main(){
40
41 cout << "Approximate travelling salesman path finder." << endl;
42 cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl << endl;
43
44 map<City, OtherCities> database;
45 ReadInData(&database);
46 ...
47 }
作为旁注,ReadInData
函数只需要一个 map<City, OtherCities>
引用作为参数,其中 City
只是字符串的类型定义(城市名称),并且 OtherCities
是一个包含 (string, int) 对的优先级队列,这些对代表其他城市及其与第一个城市的距离。
无论如何,尝试编译它会导致以下错误:
pr3.cpp: In function ‘int main()’:
pr3.cpp:45: error: invalid initialization of non-const reference of type ‘std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::priority_queue<OtherCity, std::vector<OtherCity, std::allocator<OtherCity> >, std::greater<OtherCity> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::priority_queue<OtherCity, std::vector<OtherCity, std::allocator<OtherCity> >, std::greater<OtherCity> > > > >&’ from a temporary of type ‘std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::priority_queue<OtherCity, std::vector<OtherCity, std::allocator<OtherCity> >, std::greater<OtherCity> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::priority_queue<OtherCity, std::vector<OtherCity, std::allocator<OtherCity> >, std::greater<OtherCity> > > > >*’
我在这里做错了什么,以及(除了使用禁忌全局变量之外)还有另一种保持 database
的好方法吗?在主要功能中并在其他地方填充/使用它?我不想只按值传递它的拷贝...