C++ 零碎知识 基础知识 Set相关

1. Set 基础语法,遍历输出等

注:set在C++中是自动排序且不重复的(但在python中set是无序且不重复的)

// Set 基础语法

std::set<int> aSet;  // 初始化
aSet.insert(2);  // 插入
aSet.insert(2);  // 插入
aSet.insert(3);  // 插入
aSet.insert(4);  // 插入
aSet.insert(6);  // 插入
aSet.insert(1);  // 插入
aSet.insert(999);  // 插入
aSet.erase(1);  // 擦除 (擦除不存在的元素不会引起报错)
aSet.erase(888);  // 擦除 (擦除不存在的元素不会引起报错)

/************************************/
// Set 遍历输出

set<int>::iterator it;  // 构造一个迭代器
for (it = aSet.begin(); it != aSet.end(); ++it)
{
    cout << *it << endl;
    cout << "-----" << endl;
}


/************************************/
// Set 遍历输出 集合中某个元素和它后面的一个元素

set<int>::iterator it2;  // 构造一个新迭代器
set<int>::iterator last_but_one = std::prev(aSet.end()); // 获取指向最后一个元素前面一个元素的迭代器
for (it2= aSet.begin(); it2 != last_but_one; ++it2)
{
    cout << *it2 << endl;
    cout << *std::next(it2) << endl;
    cout << "---" << endl;
}




2. 由Point组成的Set

原因:由于set是自动排序的,因此当构造一个由Point组成的set时,需要告诉编译器,Point之间应以何种方式比较大小

2.1 定义结构体 用来比较Point

// 定义一个结构体 告诉编译器如何比较Point之间的大小关系
// 它可以放在任意位置, 只要构造set时能看到它即可
struct ComparePoints {
	bool operator()(const cv::Point& lhs, const cv::Point& rhs) const {
		if (lhs.x < rhs.x) {
			return true;
		}
		else if (lhs.x == rhs.x && lhs.y < rhs.y) {
			return true;
		}
		else {
			return false;
		}
	}
};

2.2 构造并输出Set

// 初始化Set
std::set<cv::Point, ComparePoints> pointsSet;

// 添加点
cv::Point point1(1, 2);
cv::Point point2(3, 4);
cv::Point point3(5, 4);
pointsSet.insert(point1);
pointsSet.insert(point2);
pointsSet.insert(point3);


// 遍历点集合
for (auto& point : pointsSet) {
	std::cout << "(" << point.x << ", " << point.y << ")" << std::endl;
}

3. vector 转化成 set

3.1 vector<int> --> set<int>

// ******************* 方法1 *******************//

vector<int> vecInts {1, 2, 3, 4, 4, 2, 0};
std::set<int> setInts(vecInts.begin(), vecInts.end());


// ******************* 方法2 *******************//

void vecInts2setInts(const vector<int>& vecInts, set<int>& setInts)
{
    setInts.clear();
	for (const int& data: vecIns) {
		setInts.insert(data);
	}
}

3.2 vector<cv::Point> --> set<cv::Point, ComparePoints>

// ******************* 方法1 *******************//

struct ComparePoints {
	bool operator()(const cv::Point& lhs, const cv::Point& rhs) const {
		if (lhs.x < rhs.x) {
			return true;
		}
		else if (lhs.x == rhs.x && lhs.y < rhs.y) {
			return true;
		}
		else {
			return false;
		}
	}
};

vector<cv::Point> vecPoints {cv::Point(1,1),cv::Point(2,2),cv::Point(2,3),cv::Point(2,3)};
std::set<cv::Point, ComparePoints> setPoints(vecPoints.begin(), vecPoints.end());


// ******************* 方法2 *******************//

struct ComparePoints {
	bool operator()(const cv::Point& lhs, const cv::Point& rhs) const {
		if (lhs.x < rhs.x) {
			return true;
		}
		else if (lhs.x == rhs.x && lhs.y < rhs.y) {
			return true;
		}
		else {
			return false;
		}
	}
};


void vecPoints2setPoints(const vector<cv::Point>& vecPoints, set<cv::Point, ComparePoints>& setPoints)
{
	// 清空输出参数 setPoints
	setPoints.clear();

	// 将 vector 中的元素逐个插入到 set 中
	for (const cv::Point& point : vecPoints) {
		setPoints.insert(point);
	}
}

4. 两个set求交集

4.1 set<int> 求交集

set<int> getIntsIntersect(const set<int>& set1, const set<int>& set2)
{
	std::set<int> intersection;
	std::set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), std::inserter(intersection, intersection.begin()));
	return intersection;
}

4.2 set<cv::Point, ComparePoints>求交集

set<cv::Point, ComparePoints> getPointsIntersect(const set<cv::Point, ComparePoints>& setPoints1, const set<cv::Point, ComparePoints>& setPoints2)
{
	std::set<cv::Point, ComparePoints> intersection;
	std::set_intersection(setPoints1.begin(), setPoints1.end(), setPoints2.begin(), setPoints2.end(), std::inserter(intersection, intersection.begin()), ComparePoints());
	return intersection;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值