linux下练习 c++ 特殊容器、特殊函数的使用

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: //specialcontainer.cpp /*一般容器:stack,queue 特殊容器:priority_queue .push(element),.

//specialcontainer.cpp

/*一般容器:stack,queue

特殊容器:priority_queue

.push(element),.pop(),.empty()

stack:.top()

queue:.front(),.back()

priority_queue:.top()

没有迭代器

*/

#include<iostream>

#include<queue>

using namespace std;

int main()

{

	priority_queue<int> pq;

	pq.push(40);

	pq.push(20);

	pq.push(10);

	pq.push(50);

	pq.push(90);

	while(!pq.empty())

	{

		cout<<pq.top()<<' ';

		pq.pop();

	}

	cout<<endl;

	

	

	return 0;

}


 

//specialfunctions.cpp

/*一些特殊函数的用法

for_each()

copy()

copy_backward()

sort()

remove_copy_if()

find()

find_if()

count_if()

*/

#include<iostream>

#include<algorithm>

#include<string>

#include<cctype>

#include<vector>

#include "print.h"

using namespace std;

void add10(int& element)

{

	element+=10;

}

string printe(int element)

{

	cout<<element<<" ";

	return "ok";

}

class add

{

	int inc;

public:

	add(int d):inc(d){}

	void operator()(int& element)

	{

		element+=inc;

	}

};

template<typename Iter,typename Func>

void foreach(Iter ib,Iter ie,Func f)//与for_each功能一样

{

	while(ib!=ie) f(*ib++);

}

template<class Iter,class Pos>

void co(Iter ib,Iter ie,Pos p)//与copy一样功能

{

	while(ib!=ie) *(p++)=*(ib++);

}

bool func(int n)

{

		return n&1;//偶数为1

}

bool is_upper(const string& str)

{

	return isupper(str[0]);//大写开头

}

bool is_has_o(const string& str)

{

	return str.find_first_of("oO")!=string::npos;//以o开头的

}

int main()

{

	int a[5]={4,2,6,8,9};

	int b[8]={0};

	vector<int> vt(a,a+5);

	for_each(a,a+5,add10);

	for_each(a,a+5,printe);cout<<endl;

	for_each(a,a+5,add(4));//用add类实现想加多少就加多少

	for_each(a,a+5,printe);cout<<endl;

	sort(vt.begin(),vt.end());

	print(vt.begin(),vt.end());

	copy(vt.begin(),vt.end(),a);//把vt中的数据复制到a中去

	print(a,a+5,',');

    copy_backward(a,a+5,b+8);//把a中5个数据放到最后b的5个里面

	print(b,b+8);

	vector<int> v2;

	remove_copy_if(a,a+5,back_inserter(v2),func);//后插入

	//remove_copy_if(a,a+5,front_inserter(v2),func);//前插入,适用于deque

	print(v2.begin(),v2.end());

	string str[5]={"kk","hj","fg","sd","ad"};

	string *p=find(str,str+5,"sd");

	cout<<(p==str+5?"not find ":"find ")<<"sd"<<endl;//str+5 说明没找到

	p=find_if(str,str+5,is_upper);

	cout<<(p==str+5?"not find ":"find ")<<"upper first "<<endl;//str+5 说明没找到

	cout<<count_if(str,str+5,is_upper)<<endl;//统计符合条件的个数

	return 0;

}


//print.h

//print.h

#include <iostream>

using namespace std;

#ifndef print_fun

#define print_fun

template<typename T>

///显示序列数据

void print(T b,T e,char c=' ')

{

	bool isExit=false;

	while (b!=e)

	{

		cout<<*b++<<c;

		isExit=true;

	}

	if(isExit) cout<<endl;



}

template<typename K,typename V>

ostream& operator<<(ostream& o,const pair<K,V>& p)//重载输出map类型元素

{

	return o<<p.first<<':'<<p.second;

}

#endif


 

 

 

相关文章
|
1月前
|
Linux 网络安全 Docker
盘古栈云,创建带ssh服务的linux容器
创建带ssh服务的linux容器
274 146
|
5月前
|
人工智能 机器人 编译器
c++模板初阶----函数模板与类模板
class 类模板名private://类内成员声明class Apublic:A(T val):a(val){}private:T a;return 0;运行结果:注意:类模板中的成员函数若是放在类外定义时,需要加模板参数列表。return 0;
161 0
|
8月前
|
安全 C++
【c++】继承(继承的定义格式、赋值兼容转换、多继承、派生类默认成员函数规则、继承与友元、继承与静态成员)
本文深入探讨了C++中的继承机制,作为面向对象编程(OOP)的核心特性之一。继承通过允许派生类扩展基类的属性和方法,极大促进了代码复用,增强了代码的可维护性和可扩展性。文章详细介绍了继承的基本概念、定义格式、继承方式(public、protected、private)、赋值兼容转换、作用域问题、默认成员函数规则、继承与友元、静态成员、多继承及菱形继承问题,并对比了继承与组合的优缺点。最后总结指出,虽然继承提高了代码灵活性和复用率,但也带来了耦合度高的问题,建议在“has-a”和“is-a”关系同时存在时优先使用组合。
476 6
|
9月前
|
消息中间件 Linux C++
c++ linux通过实现独立进程之间的通信和传递字符串 demo
的进程间通信机制,适用于父子进程之间的数据传输。希望本文能帮助您更好地理解和应用Linux管道,提升开发效率。 在实际开发中,除了管道,还可以根据具体需求选择消息队列、共享内存、套接字等其他进程间通信方
244 16
|
10月前
|
存储 人工智能 Python
[oeasy]python061_如何接收输入_input函数_字符串_str_容器_ 输入输出
本文介绍了Python中如何使用`input()`函数接收用户输入。`input()`函数可以从标准输入流获取字符串,并将其赋值给变量。通过键盘输入的值可以实时赋予变量,实现动态输入。为了更好地理解其用法,文中通过实例演示了如何接收用户输入并存储在变量中,还介绍了`input()`函数的参数`prompt`,用于提供输入提示信息。最后总结了`input()`函数的核心功能及其应用场景。更多内容可参考蓝桥、GitHub和Gitee上的相关教程。
245 0
|
程序员 C++ 容器
在 C++中,realloc 函数返回 NULL 时,需要手动释放原来的内存吗?
在 C++ 中,当 realloc 函数返回 NULL 时,表示内存重新分配失败,但原内存块仍然有效,因此需要手动释放原来的内存,以避免内存泄漏。
|
存储 前端开发 C++
C++ 多线程之带返回值的线程处理函数
这篇文章介绍了在C++中使用`async`函数、`packaged_task`和`promise`三种方法来创建带返回值的线程处理函数。
495 6
|
Ubuntu Linux 编译器
Linux/Ubuntu下使用VS Code配置C/C++项目环境调用OpenCV
通过以上步骤,您已经成功在Ubuntu系统下的VS Code中配置了C/C++项目环境,并能够调用OpenCV库进行开发。请确保每一步都按照您的系统实际情况进行适当调整。
2364 3
|
C++
C++ 多线程之线程管理函数
这篇文章介绍了C++中多线程编程的几个关键函数,包括获取线程ID的`get_id()`,延时函数`sleep_for()`,线程让步函数`yield()`,以及阻塞线程直到指定时间的`sleep_until()`。
292 0
C++ 多线程之线程管理函数