栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++






vector

#define _CRT_SECURE_NO_WARNINGS 1

#include 
#include   // vect:携带,or:表人,携带者在数学中有向量的意思

using namespace std;

void test01()
{
	// 创建一个容器,名叫:vector
	// < 放的是要操纵的数据类型哦~ >
	// <> 后是给容器起的一个名字是
	vector v;

	// 向容器中插入数据
	v.push_back(10);  // push:v.推,back:n.后面
	v.push_back(20);  // 作用:从后面插入一个数据
	v.push_back(30);
	v.push_back(40);

	// 通过迭代器访问容器中的数据
	vector::iterator itBegin = v.begin();  // iter:重复,ator:做...工作的人或物
	// vector表示是在这个作用符下
	// ::(作用符操作符)
	// iterator(迭代器):这是一个数据类型,表示它是一个迭代器
	// 后面写的是这个迭代器的名称
	// v.begin() 表示它是一个起始迭代器,它指向容器中的第一个元素


	// v.end() 表示它是一个结束迭代器,它指向容器中的最后一个元素的下一个位置
	vector::iterator itEnd = v.end();

	// 遍历1
	while (itBegin != itEnd)
	{
		printf("%d ", *itBegin);
		++itBegin;
	}
	
	// 遍历2
	for (vector::iterator it = v.begin(); it < v.end(); ++it) {
		printf("%d ", *it);
	}
}

int main() {
	test01();

	return 0;
}

#include 
#include 

using namespace std;

int main()
{
	vector v({ 1, 2, 3 });

	// 遍历3
	for (int i = 0; i < v.size(); ++i) {
		cout << v[i] << ' ';
	}
	cout << endl;

	// 遍历4
	for (int x : v) {
		cout << x << ' ';
	}
	cout << endl;

	return 0;
}


#include 
#include 

using namespace std;

// vector容器的数据存取

void test01()
{
	vector v1;

	for (int i = 0; i < 10; ++i)
	{
		v1.push_back(i);
	}

	// 利用[]的方式来访问vector中的元素
	for (int i = 0; i < v1.size(); ++i)
	{
		cout << v1[i] << ' ';
	}
	cout << endl;

	// 利用at()成员函数来访问vector中的元素
	// 这个与[]的区别在于,它会判断是否越界
	// 所以它 1.比[]慢一点 2.更安全
	for (int i = 0; i < v1.size(); ++i)
	{
		cout << v1.at(i) << ' ';
	}
	cout << endl;

	cout << "第一个元素为:" << v1.front() << endl;  // front n.前面
	cout << "最后一个元素为:" << v1.back() << endl;  // back n.后面
}

int main()
{
	test01();

	return 0;
}

queue



#include 
#include 

using namespace std;

class Person
{
public:
	Person(string name, int age) {
		this->name = name;
		this->age = age;
	}
	string name;
	int age;
};

int main()
{
	// 创建队列容器
	queue q;

	Person p1("小白", 1);
	Person p2("小黑", 2);
	Person p3("小红", 3);
	Person p4("小灰", 4);

	q.push(p1);  // 队列中只要写push
	q.push(p2);
	q.push(p3);
	q.push(p4);

	cout << "队列的大小为:" << q.size() << endl;

	while (!q.empty())  // empty() e非,mp必须,ty表n.  不是必须的->n.空的东西
	{
		// 查看队头元素
		cout << "队头元素——姓名" << q.front().name << " 年龄" << q.front().age << 't';
		// front n.前面
		
		// 查看队尾元素
		cout << "队尾元素——姓名" << q.back().name << " 年龄" << q.back().age << endl;

		q.pop();  // 出队
	}

	cout << "队列的大小为:" << q.size() << endl;

	
	return 0;
}


#include 
#include 

using namespace std;

int main()
{
	// 创建stack容器
	stack s;

	// 入栈
	s.push(10);
	s.push(20);
	s.push(30);
	s.push(40);

	cout << "size = " << s.size() << endl;
	while (!s.empty()) {
		printf("%d ", s.top());
		s.pop();
	}
	cout << "nsize = " << s.size() << endl;

	return 0;
}

deque(double-ended queue),即“双端队列”



#include 
#include 
using namespace std;

void printDeque(const deque& d)  // 在容器前加个const,使得容器只能读
{
	for (deque::const_iterator i = d.begin(); i < d.end(); ++i) {
		printf("%d ", *i);
	}
	cout << 'n';
}

int main()
{
	deque d;
	for (int i = 0; i < 10; ++i)
	{
		d.push_back(i);
	}
	printDeque(d);

	// 新的容器(另一个容器的起始迭代器, 另一个容器的结束迭代器)
	// 作用:将另一个容器当中的值 赋给 新的容器
	deque d2(d.begin(), d.end());  
	printDeque(d2);

	deque d3(10, 666);  // (倍数, 数)
	printDeque(d3);

	deque d4(d3);
	printDeque(d4);

	return 0;
}


#include 
#include 
using namespace std;

void printDeque(const deque& d)  // 在容器前加个const,使得容器只能读
{
	for (deque::const_iterator i = d.begin(); i < d.end(); ++i) {
		printf("%d ", *i);
	}
	cout << 'n';
}

int main()
{
	deque d1;
	for (int i = 0; i < 10; ++i)
	{
		d1.push_back(i);
	}
	printDeque(d1);

	if (!d1.empty()) {
		printf("不为空,size = %dn", d1.size());
	}
	else {
		printf("为空");
	}

	// 重新指定大小
	d1.resize(15, 36);
	printDeque(d1);

	return 0;
}

set

#define _CRT_SECURE_NO_WARNINGS 1
#include 
#include 

using namespace std;

void printSet(set& s)
{
	for (set::iterator i = s.begin(); i != s.end(); ++i)
	{
		cout << *i << ' ';
	}
	cout << 'n';
}

int main()
{
	set s;
	s.insert(90);
	s.insert(80);
	s.insert(40);
	s.insert(50);
	s.insert(70);

	printSet(s);

	// erase(e-出 + ras-擦 + e -> 擦出去)擦除
	s.erase(s.begin());  // 删除迭代器指向的元素
	printSet(s);

	s.erase(90);  // 删除指定的一个数字,如果没有也不会报错
	printSet(s);

	s.erase(s.begin(), s.end());  // 删除迭代器指向的范围中的元素
	if (s.empty())
	{
		printf("空了n");
	}

	s.insert(60);
	printSet(s);

	s.clear();  // clear a.清楚的,把一个容器变清楚就是将它清空
	if (s.empty())
	{
		printf("空了n");
	}

	return 0;
}

multiset:multi(很多) + set(集合):多重集合,不同set,它可以重复出现同一个元素。

#include 
#include   // set(集合)
using namespace std;

void printSet(const set& s)
{
	// set的迭代器为双向迭代器,不支持 < 这个比较操作符
	for (set::const_iterator i = s.begin(); i != s.end(); ++i) {
		printf("%d ", *i);
	}
}

void printMultiset(multiset& ms) {
	for (multiset::iterator i = ms.begin(); i != ms.end(); ++i) {
		printf("%d ", *i);
	}
	printf("n");
}

int main()
{
	set s1;
	
	pair::iterator, bool> ret = s1.insert(10);
	if (ret.second) {
		printf("插入成功n");
	}
	else {
		printf("失败n");
	}

	ret = s1.insert(10);
	if (ret.second) {
		printf("插入成功n");
	}
	else {
		printf("失败n");
	}

	multiset ms;  // 创建一个多重集合容器
	ms.insert(10);
	ms.insert(10);
	printMultiset(ms);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include 
#include 

using namespace std;

void printSet(set& s)
{
	for (set::iterator i = s.begin(); i != s.end(); ++i)
	{
		cout << *i << ' ';
	}
	cout << 'n';
}

int main()
{
	set s;
	s.insert(90);
	s.insert(80);
	s.insert(40);
	s.insert(50);
	s.insert(70);

	printf("%dn", s.count(550));
	

	return 0;
}

lower_bound upper_bound

#define _CRT_SECURE_NO_WARNINGS 1
#include 
#include 

using namespace std;

int main()
{
	set s;
	s.insert(30);
	s.insert(80);
	s.insert(40);
	s.insert(50);
	s.insert(70);

	// lower(low a.低的 + er 表比较		下层的),bound(边界):下界
	// lower_bound:找到 >=那个元素 的最小的元素的迭代器
	set::iterator ret1 = s.lower_bound(40);

	// upper(up 向上 + er 表比较		上面的),bound(边界):上界
	// upper_bound:找到 >那个元素 的最小元素的迭代器
	set::iterator ret2 = s.upper_bound(30);
	
	printf("%dn", *ret1);
	printf("%dn", *ret2);

	return 0;
}

#define _CRT_SECURE_NO_WARNINGS 1
#include 
#include 

using namespace std;

int main()
{
	// pair(n.一对) 对组的创建
	// 第一种方式
	pair p("Jack", 18);

	// 这两个是属性,所以不用加小括号
	cout << p.first << endl;  // first a.第一的
	cout << p.second << endl;  // second a.第二的

	// 第二种方式
	pair p2 = make_pair("Tom", 88);
	
	cout << p2.first << endl;
	printf("%dn", p2.second);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include 
#include 
#include 

using namespace std;

void printMap(map& m)
{
	for (map::iterator i = m.begin(); i != m.end(); ++i)
	{
		printf("key = %dtvalue = %dn", (*i).first, i->second);
	}
}

int main()
{
	// map(地图),<>里放的是模版参数
	// 第一个参数为键值,第二个参数为实值
	map m;

	m.insert(pair(1, 10));  // 这是一个匿名的对组
	m.insert(pair(3, 30));
	m.insert(pair(2, 20));
	
	m.insert(pair(4, 40));

	printMap(m);

	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1012660.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号