我以为我可以像这样创建和填充 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
只是 string 的 typedef(城市名称),并且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> > > > >*’
我在这里做错了什么,并且(除了使用禁忌全局变量之外),还有另一种很好的方法可以将 the 保留database
在主函数中并在其他地方填充/使用它?我不想只是按价值传递它的副本......