C++ set 总结

set 是什么

set 关键字即值,即只保存关键字的容器 

multiset 关键字可重复出现的 set

自我回答 set 是存储无重复的有序且相同数据类型的容器,是一种关联容器,数据结构

set 与 multiset 区别的代码实现

#include 
#include 
#include 

using namespace std;

int main()
{
	vector ivec;

	for (int i = 0; i != 10; i++)
	{
		ivec.push_back(i);
		ivec.push_back(i);
	}

	set iset(ivec.cbegin(), ivec.cend());

	multiset miset(ivec.cbegin(), ivec.cend());

	cout << "ivec.size() = " << ivec.size() << endl;
	cout << "iset.size() = " << iset.size() << endl;
	cout << "miset.size() = " << miset.size() << endl;

	for (auto iNum : iset)
	{
		cout << iNum << " ";
	}

	cout << endl;

	for (auto iN : miset)
	{
		cout << iN << " ";
	}

	return 0;
}

C++ set 总结_第1张图片

set 与 vector 的区别的代码实现

#include 
#include 
#include 
#include 

using namespace std;

string &trans(string &s)
{
	for (int p = 0; p < s.size(); p++)
	{
		if (s[p] >= 'A' && s[p] <= 'Z')
		{
			s[p] += 32;
		}
		else if (s[p] == '.' || s[p] == ',')
		{
			s.erase(p, 1);
		}
	}

	return s;
}

int main()
{
	vector unique_word;

	string word;

	while (cin >> word)
	{
		trans(word);

		if (find(unique_word.begin(), unique_word.end(), word) == unique_word.end())
		{
			unique_word.push_back(word);
		}
	}

	for (const auto &w : unique_word)
	{
		cout << w << " ";
	}

	cout << endl;

	set unique_word2;

	string word2;

	while (cin >> word2)
	{
		trans(word2);

		unique_word2.insert(word2);
	}

	for (const auto &w2 : unique_word)
	{
		cout << w2 << " ";
	}

	cout << endl;

	return 0;
}

 

你可能感兴趣的:(C++)