常用函数和STL

本文详细介绍了C++编程中常用的函数,如输入输出、内存管理、数学运算、字符串处理等,并探讨了STL中的向量、链表、队列、栈、集合、映射等容器及其使用方法。同时,讲解了标准算法,如排序、查找、替换等,以及C++ String的相关操作。这些基础知识对于C++程序员来说至关重要。
摘要由CSDN通过智能技术生成

常用函数和STL

一. 常用函数

#include <stdio.h>
int getchar( void );               //读取一个字符, 一般用来去掉无用字符
char *gets( char *str );          //读取一行字符串
#include <stdlib.h>
void * malloc( size_t size );     //动态内存分配, 开辟大小为 size 的空间
void qsort( void *buf, size_t num, size_t size, int (*compare)(const void *, const void *) );                    //快速排序
Sample:
int compare_ints( const void* a, const void* b ) 
{
int* arg1 = (int*) a;        int* arg2 = (int*) b;
if( *arg1 < *arg2 ) return -1;
else if( *arg1 == *arg2 ) return 0;
else return 1;
}
int array[] = { -2, 99, 0, -743, 2, 3, 4 };     int array_size = 7; 
qsort( array, array_size, sizeof(int), compare_ints ); 
#include <math.h>
//求反正弦, arg∈[-1, 1], 返回值∈[-pi/2, +pi/2]
double asin( double arg );
//求正弦, arg为弧度, 弧度=角度*Pi/180.0, 返回值∈[-1, 1]
double sin( double arg );
//求e的arg次方
double exp( double arg );
//求num的对数, 基数为e
double log( double num );
//求num的根
double sqrt( double num );
//求base的exp次方
double pow( double base, double exp );
#include <string.h>
//初始化内存, 常用来初始化数组
void* memset( void* buffer, int ch, size_t count );
memset( the_array, 0, sizeof(the_array) );
//printf是它的变形, 常用来将数据格式化为字符串
int sprintf( char *buffer, const char *format, ... );
sprintf(s, "%d%d", 123, 4567); //s="1234567"
//scanf是它的变形, 常用来从字符串中提取数据
int sscanf( const char *buffer, const char *format, ... );
Sample:
char result[100]="24 hello", str[100];          int num;
sprintf( result, "%d %s", num,str );//num=24;str="hello" ;
//字符串比较, 返回值<0代表str1<str2, =0代表str1=str2, >0代表str1>str2
int strcmp( const char *str1, const char *str2 );

二. 常用STL

[标准container概要]
vector<T>				大小可变的向量, 类似数组的用法, 容易实现删除
list<T>					双向链表
queue<T>				队列, empty(), front(), pop(), push()
stack<T>, empty(), top(), pop(), push()
priority_queue<T> 		优先队列, empty(), top(), pop(), push()
set<T>					集合
map<key,val>			关联数组, 常用来作hash映射
[标准algorithm摘录]
for_each()				对每一个元素都唤起(调用)一个函数
find()            		查找第一个能与引数匹配的元素
replace()         		用新的值替换元素, O(N)
copy()            		复制(拷贝)元素, O(N)
remove()				移除元素
reverse()				倒置元素
sort()            		排序, O(N log(N))
partial_sort()     		部分排序
binary_search()			二分查找
merge()           		合并有序的序列, O(N)

[C++ String摘录]
copy() 					从别的字符串拷贝
empty() 				判断字符串是否为空
erase() 					从字符串移除元素
find()					查找元素
insert()					插入元素
length()					字符串长度
replace()				替换元素
substr() 				取子字符串
swap()					交换字符串
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值