【c++】知识点

*关于两个不同类型的操作数进行运算时的默认类型转换:

C在以下四种情况下会进行隐式转换:
       1、算术运算式中,低类型能够转换为高类型。
       2、赋值表达式中,右边表达式的值自动隐式转换为左边变量的类型,并赋值给他。
       3、函数调用中参数传递时,系统隐式地将实参转换为形参的类型后,赋给形参。
       4、函数有返回值时,系统将隐式地将返回表达式类型转换为返回值类型,赋值给调用函数。

标准C++中的string类的用法总结【转 】【部分常用】

比较(compare)

语法:

  int compare( const basic_string &str );
  int compare( const char *str );
  int compare( size_type index, size_type length, const basic_string &str );
  int compare( size_type index, size_type length, const basic_string &str, size_type index2,
  size_type length2 );
  int compare( size_type index, size_type length, const char *str, size_type length2 );

compare()函数以多种方式比较本字符串和str,返回:

返回值情况
小于零this < str
this == str
大于零this > str

不同的函数:

  • 比较自己和str,
  • 比较自己的子串和str,子串以index索引开始,长度为length
  • 比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己
  • 比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串以index开始,长度为length

删除(erase)

语法:

  iterator erase( iterator pos );
  iterator erase( iterator start, iterator end );
  basic_string &erase( size_type index = 0, size_type num = npos );

erase()函数可以:

  • 删除pos指向的字符, 返回指向下一个字符的迭代器,
  • 删除从start到end的所有字符, 返回一个迭代器,指向被删除的最后一个字符的下一个位置
  • 删除从index索引开始的num个字符, 返回*this.

参数index 和 num 有默认值, 这意味着erase()可以这样调用:只带有index以删除index后的所有字符,或者不带有任何参数以删除所有字符. 例如:

 

    string s("So, you like donuts, eh? Well, have all the donuts in the world!");
    cout << "The original string is '" << s << "'" << endl;
  
    s.erase( 50, 14 );
    cout << "Now the string is '" << s << "'" << endl;

    s.erase( 24 );
    cout << "Now the string is '" << s << "'" << endl;

    s.erase();
    cout << "Now the string is '" << s << "'" << endl;

将显示

    The original string is 'So, you like donuts, eh? Well, have all the donuts in the world!'
    Now the string is 'So, you like donuts, eh? Well, have all the donuts'
    Now the string is 'So, you like donuts, eh?'
    Now the string is ''

查找(find)

语法:

  size_type find( const basic_string &str, size_type index );
  size_type find( const char *str, size_type index );
  size_type find( const char *str, size_type index, size_type length );
  size_type find( char ch, size_type index );

find()函数:

  • 返回str在字符串中第一次出现的位置(从index开始查找)如果没找到则返回string::npos,
  • 返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos,
  • 返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos

例如,

 

    string str1( "Alpha Beta Gamma Delta" );
    unsigned int loc = str1.find( "Omega", 0 );
    if( loc != string::npos )
      cout << "Found Omega at " << loc << endl;
    else
      cout << "Didn't find Omega" << endl;

插入(insert)

语法:

  iterator insert( iterator i, const char &ch );
  basic_string &insert( size_type index, const basic_string &str );
  basic_string &insert( size_type index, const char *str );
  basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );
  basic_string &insert( size_type index, const char *str, size_type num );
  basic_string &insert( size_type index, size_type num, char ch );
  void insert( iterator i, size_type num, const char &ch );
  void insert( iterator i, iterator start, iterator end );

insert()函数的功能非常多:

  • 在迭代器i表示的位置前面插入一个字符ch,
  • 在字符串的位置index插入字符串str,
  • 在字符串的位置index插入字符串str的子串(从index2开始,长num个字符),
  • 在字符串的位置index插入字符串str的num个字符,
  • 在字符串的位置index插入num个字符ch的拷贝,
  • 在迭代器i表示的位置前面插入num个字符ch的拷贝,
  • 在迭代器i表示的位置前面插入一段字符,从start开始,以end结束.

替换(replace)

语法:

  basic_string &replace( size_type index, size_type num, const basic_string &str );
  basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2,
  size_type num2 );
  basic_string &replace( size_type index, size_type num, const char *str );
  basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );
  basic_string &replace( size_type index, size_type num1, size_type num2, char ch );
  basic_string &replace( iterator start, iterator end, const basic_string &str );
  basic_string &replace( iterator start, iterator end, const char *str );
  basic_string &replace( iterator start, iterator end, const char *str, size_type num );
  basic_string &replace( iterator start, iterator end, size_type num, char ch );

replace()函数:

  • 用str中的num个字符替换本字符串中的字符,从index开始
  • 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符
  • 用str中的num个字符(从index开始)替换本字符串中的字符
  • 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符
  • 用num2个ch字符替换本字符串中的字符,从index开始
  • 用str中的字符替换本字符串中的字符,迭代器start和end指示范围
  • 用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围,
  • 用num个ch字符替换本字符串中的内容,迭代器start和end指示范围.

例如,以下代码显示字符串"They say he carved it himself...find your soul-mate, Homer."

    string s = "They say he carved it himself...from a BIGGER spoon";
    string s2 = "find your soul-mate, Homer.";

    s.replace( 32, s2.length(), s2 );

    cout << s << endl;
  

substr

语法:

  basic_string substr( size_type index, size_type num = npos );

substr()返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串。

例如:

    string s("What we have here is a failure to communicate");

    string sub = s.substr(21);

    cout << "The original string is " << s << endl;
    cout << "The substring is " << sub << endl;

显示:

    The original string is What we have here is a failure to communicate
    The substring is a failure to communicate

交换(swap)

语法:

  void swap( basic_string &str );

swap()函数把str和本字符串交换。例如:

    string first( "This comes first" );
    string second( "And this is second" );
    first.swap( second );
    cout << first << endl;
    cout << second << endl;

显示:

    And this is second
    This comes first

C++中的常用数学函数使用总结

1.开平方

double sqrt(double x);

2.求常数e的x次方

double exp(double x);

3.求x的y次方

double pow(double x, double y);

4.求对数ln(x)

double log(double x);

   求对数lg(x)

double log10(double x);

  其他用换底公式

5.求x绝对值

int abs(x);

long int abs(long int x);

double fabs(double x);

6.三角函数

   求正弦 

double sin(double x);

   求余弦

double cos(double x);

   求正切

double tan(double x);

   反正切

double atan(double x);

7.取整函数

   向上取整

double ceil(double x);

   向下取整

double floor(double x);

8.产生随机数 0~32767

int rand(void);

动态申请二维数组 
    int n,m;
    cin>>n>>m;
    int ** aa=new int*[m];
    for(int i=0;i<m;i++){
        aa[i]=new int[n];
    }

sort函数的使用 

#include<algorithm>

bool compare(int a,int b){
	return a>b;
}
int main(){

	
	//sort函数的使用 
	int a[10]={4,1,2,5,8,9,7,6,3,0};
	sort(a,a+10,compare);
	for(int i=0;i<10;i++){
		cout<<a[i]<<" ";
	} 
	
} 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值