C/C++常见头文件

仅供学习笔记使用,侵删

#include<bits/stdc++.h>	//万能开头 
#include<stdio.h>	//C
#include<iostream>	//C++
#include<math.h>	//abs,pow
#include<string.h>	//strcmp,strlen 
#include<stack> 	//stl 
#include<queue>		//bfs广度优先遍历 
#include<vector>	//
#include<map>		//键值对映射 
#include<set>		//不重复的统计 
#include<algorithm>	//sort
using namespace std;

struct student{
	string name;
	int grade1;
	int grade2;	
};

void Use_vector(){
	vector<int> v;//指定一维数组大小vector<int> v(20);
	vector<int> vv[5]; //二维数组
	
	int a[10] = {1,2,3,5,7};
	for(int i = 0; i < 5; i++)
		v.push_back(a[i]);
	for(int i = 0; i < 5; i++)
		cout << "v[i]=" << v[i] << endl;
		//弹出元素v.pop_back()
		//取出最后一个元素*(v.end()-1)
	cout << "v.size=" << v.size() << endl <<endl;
	
	v.pop_back();
	cout << "v.size=" << v.size() << endl << endl;	
}

void Use_map(){
	map<int, string> m;
	m[1] = "Alice";
	m[2] = "Bob";
	m.insert(pair<int, string>(3,"Cindy"));
	for(int i = 1; i <= 3; i++)
		cout << m[i] <<endl;
}

void Use_set(){
	set<int> s;
	s.insert(1);
	s.insert(2);
	s.insert(3);
	s.insert(1);
	if(!s.empty())
		cout << s.size() << endl<< "s.begin=" << *s.begin() << "      s.end=" << *s.end() << endl;
	s.clear();
	cout << s.size() <<endl;
}

void Use_string(){
	string s = "ab";
	string ss = "cbe";
	string str = s+ss;
	cout << s+ss << endl;
	cout << s+"cbe" << endl;
	cout << str[1] << endl;
	cout << str.length() << endl;
	str = "ABCDEABCDE";
	cout << str.find('A') << endl << str.find('AB',3) <<endl;//指定起始位置开始找
}

bool cmp1(int a, int b){		//a是否小于b
	return a > b;
}
bool cmp2(student s1,student s2){		//s1是否小于s2
	if(s1.grade1 != s2.grade1){
		return s1.grade1 > s2.grade1;	//不能加等于,只能小于或者大于
	}else{
		return s1.grade2 > s2.grade2; 
	}
} 
void Use_algorithm(){
	int a[5] = {1,5,3,2,4};
	for(int i = 0; i < 5; i++)	cout << a[i] << " "; 
	cout << endl;
	
	sort(a,a+5);	//默认升序,  sort(first_pointer, first_pointer+n, cmp)  !!n是数组长度!! 
	for(int i = 0; i < 5; i++)	cout << a[i] << " "; 
	cout << endl;
	
	sort(a, a+5, cmp1);//自定义比较方法,返回值是bool类型的 
	for(int i = 0; i < 5; i++)	cout << a[i] << " "; 
	cout << endl;
	
	student s[5];
	s[0].name = "Alice";	s[0].grade1 = 75;	s[0].grade2 = 80;
	s[1].name = "Bob";		s[1].grade1 = 75;	s[1].grade2 = 70;
	s[2].name = "Cindy";	s[2].grade1 = 90;	s[2].grade2 = 60;
	s[3].name = "Dili";		s[3].grade1 = 75;	s[3].grade2 = 90;
	s[4].name = "Eric";		s[4].grade1 = 75;	s[4].grade2 = 100;
	sort(s,s+5,cmp2);
	for(int i = 0; i< 5; i++){
		cout<< s[i].name << "   garde1=" << s[i].grade1 << "    grade2=" << s[i].grade2 << endl;
	}
	
}

void Use_math(){
	double x = -2.3;
	cout << fabs(x) << endl;
	cout << pow(2,3) << endl << ceil(x) <<endl << floor(x) <<endl;
}

void Use_sstream(){
	string s = "3.14 hello 5";
	double x;
	string str;
	int y;
	stringstream ss;
	ss << s;
	ss >> x >> str >> y;
	cout << "x=" << x << "   str=" << str <<"    y=" << y << endl;
}

int main(){
//	freopen("d:work\\in.txt","r",stdin);
//	freopen("d:work\\out.txt","w",stdout);
//	while(~scanf("%lf%lf", &now_price, &discount))
//	fclose(stdin);
//	fclose(stdout);

	Use_vector();
	Use_map();	
	Use_set();
	Use_string();
	Use_algorithm();
	Use_math();
	Use_sstream();
	
	return 0;
} 
 
 
 
 
 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值