我是靠谱客的博主 跳跃金毛,最近开发中收集的这篇文章主要介绍C++STL容器篇(三)集合映射列表元祖,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

目录

集合

映射

列表

元祖


集合

set/multiset/bitset

#include<set>
#include<iostream>
#include<ctime>
#include<string>
using namespace std;
/*
set:集合
数据自带排序性
数据唯一性
*/
class Mickey {
public:
Mickey(string name,int age):name(name),age(age){}
void print() { cout << name << "
" << age << endl; }
bool operator<(const Mickey& object) const{ return (this->name )< (object.name); }
protected:
string name;
int age;
};
void testSet() //
{
srand((unsigned int)time(nullptr));
set<int>setData;
//默认方式从小到大,同下
//set<int,less<int>>setData;
和默认方式一样
//set<int,greater<int>>setData;
从大到小
for (int i = 0; i < 10; i++) {
setData.insert(rand() % 10);
}
for (auto v : setData)
cout << v << "
";
cout << endl;
}
void testMickey() {
set<Mickey>data;
data.insert(Mickey("name3", 18));
data.insert(Mickey("name4", 15));
data.insert(Mickey("name1", 30));
for (auto v : data)
v.print();
}
//multiset:多重集合,只具有排序性,不具有去重功能
void testMultiset() {
multiset<int>data;
srand((unsigned int)time(nullptr));
for (int i = 0; i < 10; i++)
data.insert(rand() % 10);
for (auto v : data)
cout << v << "
";
cout << endl;
}
int main() {
testSet();
testMickey();
testMultiset();
return 0;
}
#include<iostream>
#include<bitset>
using namespace std;
void testBitset() {
//多个二进制位
bitset<8>data(00111110);
cout << data << endl;
data.flip();
cout << data << endl;
cout << data.all() << endl;
cout << data.any() << endl;
cout << data.size() << endl;
cout << data.none() << endl;
bitset<8>data1(8);
cout << data1<< endl;
}
int main() {
testBitset();
return 0;
}

 

映射

map/multiset

#include<iostream>
#include<map>
#include<string>
using namespace std;
/*
map:自带排序性,默认从小到大
具有唯一性
*/
template<class _Ty1,class _Ty2>
class myPair {
public:
myPair(_Ty1 first,_Ty2 second):first(first),second(second) {}
protected:
_Ty1 first;
_Ty2 second;
};
void testMap() {
map<int ,string>data;
//insert插入
data.insert(pair<int, string>(1, "name1"));
//make_pair构建数对插入
data.insert(make_pair<int, string>(2, "name2"));
//单映射可以直接采用数组下标方式插入
//数组才一定程序下可以当做数对
//map[first]=second;
data[4] = string("name4");
cout << data[4] << endl;//用的时候直接用即可,相比数组来说这个下标没有任何要求
for (auto iter = data.begin(); iter != data.end(); iter++)
cout << iter->first << " " << iter->second << "
";
cout << endl;
for (auto v : data)
cout << v.first << "
" << v.second << "
";
cout << endl;
data.erase(1);//删除函数
for (auto v : data)
cout << v.first << "
" << v.second << "
";
cout << endl;
}
class Boy {
public:
Boy() = default;
Boy(string name,int age):name(name),age(age){}
void print() const{ cout << name << "
" << age <<"
"; }
bool operator<(const Boy& boy)const {return
this->name < boy.name; }
protected:
string name;
int age;
};
class Girl {
public:
Girl() = default;
Girl(string name, int age) :name(name), age(age) {}
void print()const { cout << name << "
" << age<<"
"; }
protected:
string name;
int age;
};
void testDIY() {
map<Boy, Girl>data;
data[Boy("小杰", 12)] = Girl("小美", 23);
data[Boy("小凯", 25)] = Girl("小筱", 28);
data[Boy("小俊", 16)] = Girl("小芳", 33);
for (auto v : data)
{
v.first.print();
v.second.print();
cout << endl;
}
}
void testMultimap() {
multimap<int, string>data;
data.insert(pair<int, string>(12,"小洁" ));
data.insert(pair<int, string>(12, "小君"));
data.insert(pair<int, string>(23, "小理"));
for (auto v : data)
cout << v.first << "
" << v.second << endl;
}
int main() {
testMap();
testDIY();
testMultimap();
return 0;
}

列表

initializer_list

#include<iostream>
#include<array>
#include<list>
#include<vector>
#include<initializer_list>
using namespace std;
class Obtain {
public:
Obtain(string a,string b,string c):a(a),b(b),c(c){}
Obtain(const initializer_list<string>& list) {
for (auto iter = list.begin(); iter != list.end(); iter++)
cout << *iter << "
";
cout << endl;
}
protected:
string a;
string b;
string c;
};
void print(const initializer_list<int>& list) {
for (auto iter = list.begin(); iter != list.end(); iter++)
cout << *iter << "
";
}
int main() {
array<int, 3>data={1, 2, 3};
vector<int>data1= {1, 2, 3, 4, 5};
vector<int>data2= {1, 2, 3, 4};
Obtain data3={"string1", "string2", "string3"};
Obtain data4 = { "string1", "string2" };
Obtain data_4 = { "string1" };
initializer_list<string>data5 = { "string1","string2","string3" };
print({ 1 });
print({ 1,2 });
print({ 1,2,3 });
print({ 1,2,3,4 });
return 0;
}

元祖

tuple

#include<iostream>
#include<tuple>
using namespace std;
void testTuple() {
// 把任何类型的数据当做一组处理
tuple<string, int, int, string>data = { "小杰",12,10,"1234" };
tuple<double, double, double>score = { 98,98,89 };
tuple<string, string>data1 = forward_as_tuple("小张", "小美");
tuple<string, int, int, string>a[3];
//get方式访问不能用for循环
cout << get<0>(data) << "
";
cout << get<1>(data) << "
";
cout << get<2>(data) << "
";
cout << get<3>(data) << "
";
cout << endl;
//tie方式访问
string name;
int age;
int num;
string tel;
tie(name, age, num, tel) = data;
cout << name << "
" << age << "
" << num << "
" << tel << endl;
}
void EXoperator() {
tuple<string, int, int, string>data = { "小杰",12,10,"1234" };
tuple<double, double, double>score = { 98,98,89 };
auto result = tuple_cat(data, score);//把两组数据链接
cout << get<0>(result) << "
";
cout << get<1>(result) << "
";
cout << get<2>(result) << "
";
cout << get<3>(result) << "
";
cout << get<4>(result) << "
";
cout << get<5>(result) << "
";
cout << get<6>(result) << "
";
}
int main() {
testTuple();
EXoperator();
return 0;
}

 

最后

以上就是跳跃金毛为你收集整理的C++STL容器篇(三)集合映射列表元祖的全部内容,希望文章能够帮你解决C++STL容器篇(三)集合映射列表元祖所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(87)

评论列表共有 0 条评论

立即
投稿
返回
顶部