C++有点用的小知识(持续更新)

1.length()函数

只用来获取字符串的长度。
变量名.length()

2.size()函数

既可以获取字符串的长度也可以获取vector类型的长度。
变量名.size()

3.sizeof()运算符

用来求对象所占内存空间的大小。
sizeof(变量名)

4.计时函数clock()

获取程序运行的时间。

#include <ctime>
...
double s=clock();
...
printf("Time used=%.2f\n",(double)(clock()-s)/CLOCKS_PER_SEC);
return 0;

除以常数CLOCKS_PER_SEC,时间以秒为单位。

5.floor()函数

用于四舍五入。
floor(x+0.5)

6.变量交换

交换两个整型、浮点型和字符型变量。

a^=b^=a^=b;

7.system(“pause”)

不让程序“按任意键退出”

8.memset()函数

清零数组。

#include <string.h>
...
memset(a,0,sizeof(a));//清零数组a。

9.memcpy()函数

从数组复制元素到数组b。

#include <string.h>
...
memcpy(b,a,sizeof(a));//全部复制
...
mencpy(b,a,sizeof(int)*k);//复制k个原素

10.sort()函数

实现数组排序

#include <algorithm>
...
int a[100];
...
sort(a,a+100,less<int>());//从小到大排序
sort(a,a+100,greater<int>());//从大到小排序

11.C++11 raw string literal 技术

直接复制粘贴输出字符

#include <iostream>
using namespace std;
...
cout<<R"(//复制粘贴字符串)";

12.64位编译器的各种整型范围

unsigned int:0~4294967295(0~2^32-1)
(10^10)

int:-2147483648~2147483647(-2^31 2^31-1)
(10^10)

unsigned long:0~4294967295(0~2^32-1)
(10^10)

long:-2147483648~2147483647(-2^31 - 2^31-1)
(10^10)

long long:-9223372036854775808-9223372036854775807(-2^63 -2^63-1) (10^19)

unsigned long long:0-18446744073709551615(0~2^64-1)
(10^20)

13.reverse()函数

翻转数组中的元素

#include <iostream>
using namespace std;
#include <algorithm>
int a[5]={1,2,3,4,5};
...
reverse(a,a+5);
...

14.数值转字符串

方法一:
string to_string (int val);
string to_string (double val);
例如:

string s = to_string(123);

方法二:
stringstream (使用stringstream 需包含头文件 “sstream”)
例如:

#include <sstream>
...
int a = 123, b = 456, c = 789;
stringstream ss;
ss << a << b << c;
...

注意:stringstream型不能直接输出,也不支持迭代器,需要利用stringstream类的成员函数str()才能把它当成普通字符串来用,例如:

string s = ss.str();

字符串转为数值也可以用stringstream
例如:

int i;
ss >> i;

方法三:
此方法在oj平台无法使用
使用itoa()函数
函数原型 char *itoa( int value, char *string,int radix);
第一个参数是要转换的数字,第二个参数是要写入转换结果的目标字符串,第三个参数是转换数字时所用的基数。
例如:

#include <stdlib.h>
...
int num = 10;
char str[100];
itoa(num, str, 8); //将10转化为八进制存入字符数组

字符转回数值:
例如:将十进制转化为二进制

int n = atoi(itoa(num, str, 2));   //先把num转换为二进制的字符串,再把该字符串转换为整数

15.count()函数

count(ivec.begin() , ivec.end() , searchValue);
作用:
统计在一定范围内某一值出现的次数
前两个参数为起始位置和结束位置,都是迭代器,第三个参数为需要统计的值
例如:

 count(s.begin(), s.end(), x + '0');

16.malloc()函数

函数原型:void *malloc(unsigned int size)

作用:在内存的动态存储区中分配一个长度为size的连续空间。

参数:内存块的大小,以字节为单位。

返回值:
成功时,指向函数分配的内存块的指针。
该指针的类型始终为void*,可以将其强制转换为所需的数据指针类型,以便将其取消引用。
如果函数未能分配所请求的内存块,则返回空指针。

/* malloc example: random string generator*/
#include <iostream>
using namespace std;
#include <stdlib.h>     /* malloc, free, rand */
int main()
{
    int i, n;
    char* buffer;

    cout << "How long do you want the string? "<<endl;
    cin >> i;
    buffer = (char*)malloc(i+1);//申请空间,并进行类型转换
    if (buffer == NULL) exit(1);

    for (n = 0; n < i; n++)
        buffer[n] = rand() % 26 + 'a';
    buffer[i] = '\0';

   cout<<"Random string:"<< buffer;
    free(buffer);//释放空间

    return 0;
}

该程序生成用户指定长度的字符串,并用字母字符填充。

发布了23 篇原创文章 · 获赞 31 · 访问量 1099

猜你喜欢

转载自blog.csdn.net/weixin_45333771/article/details/103216249