c++迭代器

map迭代器
以std::map> point_map;为例

std::map<std::string, std::vector<cv::Point>>    point_map;
std::vector<cv::Point>  a(2),b(3);
a[0]=cv::Point(10,10);
a[1]=cv::Point(-10,-10);
b[0]=cv::Point(1000,1000);
b[1]=cv::Point(-1000,-1000);
b[2]=cv::Point(9990,99);

//生成迭代器
std::map<std::string, std::vector<cv::Point>>::iterator mapit;//map迭代器
//用迭代器访问map中的元素
for (mapit = point_map.begin(); mapit != point_map.end(); mapit++) {
	std::string name=mapit->first;//用来访问该map
	for(int i=0;i<point_map[name].size();i++){
		std::cout<<point_map[name][i].x<<","<<point_map[name][i].y<<std::endl;
	}
	std::cout<<std::endl;
}

实例程序

#include
#include

//using namespace std;
//using namespace cv;


int main()
{
	std::map<std::string, std::vector<cv::Point>>    point_map;
	std::vector<cv::Point>  a(2), b(3);
	a[0] = cv::Point(10, 10);
	a[1] = cv::Point(-10, -10);
	b[0] = cv::Point(1000, 1000);
	b[1] = cv::Point(-1000, -1000);
	b[2] = cv::Point(9990, 99);
	
	point_map["a"] = a;
	point_map["b"] = b;


	//生成迭代器
	std::map<std::string, std::vector<cv::Point>>::iterator mapit;//map迭代器
	//用迭代器访问map中的元素
	for (mapit = point_map.begin(); mapit != point_map.end(); mapit++) {
		std::string name = mapit->first;//用来访问该map
		std::cout << name << std::endl;
		for (int i = 1; i < point_map[name].size(); i++) {
			std::cout << point_map[name][i].x << "," << point_map[name][i].y << std::endl;
		}
		std::cout << std::endl;
	}

	cv::waitKey(0);
	return 0;
}



你可能感兴趣的:(c++,visual,studio,opencv)