C++ 常用方法

memest 初始化、置零、清空

extern void *memset(void buffer, int c, size_t count)
功能:将已开辟内存空间 buffer 的首 count 个字节的值设为值 c
buffer:为指针或数组
c:赋给buffer的值
count:buffer的长度

// 数组置 0
int a[50];
memset(a, 0, 50 * sizeof(int));
memset(a, 0, sizeof(a));
// 字符串初始化
char b[100];
memset(b, '/0', sizeof(b));

INT_MAX、INT_MIN 最大最小整数

− I N T _ M A X − 1 = I N T _ M I N s h o r t ≤ i n t ≤ l o n g ≤ l o n g   l o n g -INT\_MAX-1 = INT\_MIN \\ short \le int \le long \le long\ long INT_MAX1=INT_MINshortintlonglong long
short 至少 16 位,long 至少 32 位,long long 至少 64 位;
位(bit) 字节(byte) 1 字节 = 8 位

int 和 string 转换

  • int 转换成 string: to_string (C++ 11 新增)
    string to_string (int val);
    几乎可以直接转所有的数据类型为 string
  • string 转换成 int: atoi、atof、atol
    int atoi(const char* str);
int a = 123;
string b = to_string(a);

string c = "123";
int d = atoi(c.c_str());

string 常用方法

  • substr 子串
    s.substr(pos, n); // 从pos开始的n个字符的拷贝(pos的默认值是0,n的默认值是s.size() - pos)
string s("123456789");
string a = s.substr(0,5);  //获得字符串s中从第0位开始的长度为5的字符串
  • find 查找
// 查找第一个字符 'c'
char stl[] ="abcdef";
char * p = find(stl, stl + strlen(stl), 'c');

string s("abcdefg");
string::size_type position;
position = s.find("cd"); // 没找到返回string::npos
if (position!=string::npos)
	cout << position << endl;

// 在容器中查找
std::vector<int> myvector{10,20,30,40,50};
std::vector<int>::iterator it;
it = find(myvector.begin(), myvector.end(), 30);
  • reverse 反转
string str("abcde");
reverse(str.begin(), str.end());
  • 字符串转字符数组
string a = "abc123";
const char *b; //这里必须为const char *,不能用char *
b = a.c_str();

char *c = new char[20];
strcpy(c, a.c_str()); //指针类型可以不用const char *
  • 字符串比较
string A ("aBcdef");
string B ("AbcdEf");
int m=A.compare (B); //完整的A和B的比较
int n=A.compare(1,5,B,4,2); //"Bcdef"和"AbcdEf"比较
int p=A.compare(1,5,B,4,2); //"Bcdef"和"Ef"比较
int q=A.compare(1,5,B); //"Bcdef"和B比较
//若参与比较的两个串值相同,则函数返回 0;
//若字符串 A 按字典顺序要先于 B,则返回负值;反之,则返回正值。

bitset

在 bitset 头文件中,一种类似数组的结构,它的每一个元素只能是0或1,每个元素仅用1bit 空间。

#include <bitset>
bitset<8> bitset1(12); // 长度为8,用12初始化,前面补0
bitset<8> bitset2(string("10111001")); // 01字符串初始化
// 若初始值比bitsize大,参数为整数时取后面部分,参数为字符串时取前面部分
  • bitset 重载了 |、&、^、~、<<、>> 操作符
  • bitset 可用 [] 访问元素(不检查越界)
  • bitset 可用 .to_string() 转为字符串,.to_ulong() 转为无符号长整型
// 一些实用方法
bitset<8> foo ("10011011");

foo.count(); //求bitset中1的位数
foo.size(); //求bitset的位数

foo.test(2); //查下标处的元素是否为1
foo.any(); //查bitset中是否有1
foo.none(); //查bitset中是否没有1
foo.all();  //查bitset中是全部为1

foo.flip(); //将bitset每一位全部取反
foo.flip(2);  //将参数位取反
foo.set(); //将bitset的每一位全部置为1
foo.set(3,0); //将第一参数位的元素置为第二参数的值
foo.set(3); //将参数下标处置为1
foo.reset(2); //将参数下标处置为0
foo.reset(); //将bitset的每一位全部置为0

C++ Reference
bitset 用法

sort 自定义排序规则

sort() 方法第三个参数 compare,是个自定义的比较函数的指针,原型如下:
bool cmp (const Type1 &a,const Type2 & b);

sort(arr.begin(),arr.end(),less<int>()); // 默认从小到大
sort(arr.begin(),arr.end(),greater<int>());

static bool cmp(const int& a, const int& b)
{
	return a > b; //从大到小排序
}
sort(nums.begin(), nums.end(), cmp);

sort(nums.begin(), nums.end(), [](const int& a, const int& b) { return a > b; });

Vector 合并

使用 insert 或 merge(merge 要预留足够空间)

vec3.insert(vec3.end(),vec1.begin(),vec1.end())
vec3.insert(vec3.end(),vec2.begin(),vec2.end())

vec3.resize(vec1.size()+vec2.size())
merge(vec1.begin(),vec1.end(),vec2.begin(),vec2.end(),vec3.begin())

resize/reserve:前者分配空间,后者预留空间

ACM 复杂输入

  1. 形如 [[1,2,5],[1,3,5],[4,2,10]] 二维矩阵输入:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
int main()
{
	//[[1,2,5],[1,3,5],[4,2,10]]
	string res;
	cin >> res;
	stringstream ss;
	replace(res.begin(), res.end(), '[', ' ');
	replace(res.begin(), res.end(), ']', '\n');
	replace(res.begin(), res.end(), ',', ' ');
	ss << res;
	string st;
	vector<vector<int>> data;
	while (getline(ss, st)) {
		std::istringstream iss(st);
		string num;
		vector<int> tdata;
		while (iss >> num) {
			tdata.push_back(stoi(num));

		}
		data.push_back(tdata);
	}
	return 0;
}
  1. 形如 1 2 4 56 2 77 32 不定个数数字
    注意不要用 while(cin.get()!=’\0’),会导致第一个数进不去!
    while(cin>>t) 不能在结束时自动终止
vector<int> num;
int t;
do{
	cin >> t;
    num.push_back(t);
    } while (cin.get() != '\n');

判断字符类型

函数的声明在头文件 <cctype>
bool isalpha(char c) 判断字符是否为字母
bool isdigit(char c) 判断字符是否为数字
bool isalnum(char c) 判断字符是否为数字或者字母
bool islower(char c) 判断 字符是否为小写字母
bool isupper(char c) 判断 字符是否为大写字母
char tolower(char c) 把字符转化为小写字母
char toupper(char c) 把字符转化为大写字母

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值