首页 > 应用开发 > > C++ STL - unordered_map
IDE:vs2015
1 #include<unordered_map>
2 #include<string>
3 #include<vector>
4 #include<iostream>
5
6 using namespace std;
7
8 class Point
9 {
10 public:
11 Point(int id, int x, int y) :id_(id), x_(x), y_(y)
12 {
13 cout << " id_ = " << id_ << " x_ = " << x_ << " y_ = " << y_ << endl;
14 }
15
16 Point(const Point& p)
17 {
18 this->id_ = p.id_;
19 this->x_ = p.x_;
20 this->y_ = p.y_;
21 }
22
23 ~Point() {};
24 int id_;
25
26 private:
27 int x_, y_;
28 };
29
30 void dosomething(Point &points_)
31 {
32 unordered_map<int, Point> unordmap_points;
33 if (unordmap_points.find(points_.id_) == unordmap_points.end())
34 {
35 cout << "hello!" << endl;
36 unordmap_points.insert(make_pair(points_.id_, points_));
37 }
38 else
39 {
40
41 //unordmap_points[points_.id_] = points_;
42 cout << "goo-bye" << endl;
43 }
44 }
45
46 int main()
47 {
48 vector<Point> points;
49 const int maxnum[5] = {0,1,2,3,4};
50 for (int i : maxnum)
51 {
52 points.push_back(Point(i, i, i));
53 }
54
55 for (int i : maxnum)
56 {
57 dosomething(points[i]);
58 }
59 return 1;
60 }
取消注释41行后:编译器报错
:
错误 C2512 “Point::Point”: 没有合适的默认构造函数可用 TEST1 d:\program files (x86)\microsoft visual studio 14.0\vc\include\tuple 1180
请问该怎么改?
另外,个人觉得 函数 dosomething() 中的 else情形是多余的?不知道为什么,看到某底层代码有else,搞不懂,来个大神解释一波哈?
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4741/viewspace-3165175/ ,如需转载,请注明出处,否则将追究法律责任。