• 【编程】:记录一些函数


    反转字符串:algorithm中的reverse函数

    #include <iostream>
    #include <string>
    #include <algorithm>
    using namespace std;
    int main()
    {
         string s= "hello";
     
         reverse(s.begin(),s.end());
     
         cout<<s<<endl;
         return 0;
    }

     C++中stirng大小比较

    compare()函数

    类 basic_string 的成员函数 compare() 的原型如下:

    int compare (const basic_string& s) const;
    int compare (const Ch* p) const;
    int compare (size_type pos, size_type n, const basic_string& s) const;
    int compare (size_type pos, size_type n, const basic_string& s,size_type pos2, size_type n2) const;
    int compare (size_type pos, size_type n, const Ch* p, size_type = npos) const;

    如果在使用 compare() 函数时,参数中出现了位置和大小,比较时只能用指定的子串。例如:

    s.compare {pos,n, s2);

    若参与比较的两个串值相同,则函数返回 0;

    若字符串 S 按字典顺序要先于 S2,则返回负值;

    反之,则返回正值。

    下面举例说明如何使用 string 类的 compare() 函数。

    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
    {
    	string A ("aBcdef");
    	string B ("AbcdEf");
    	string C ("123456");
    	string D ("123dfg");
    	//下面是各种比较方法
    	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=C.compare(0,3,D,0,3); //"123"和"123"比较
    	cout << "m = " << m << ", n = " << n <<", p = " << p << ", q = " << q << endl;
    	cin.get();
    	return 0;
    }
    

    程序的执行结果为:

    m = 1, n = -1, p = -1, q = 0

    比较运算符

    String 类的常见运算符包括 >、<、==、>=、<=、!=。其意义分别为"大于"、"小于"、"等于"、"大于等于"、"小于等于"、"不等于"。

    举例说明:

    #include <iostream>
    #include <string>
    using namespace std;
    void TrueOrFalse (int x)
    {
    	cout << (x?"True":"False")<<endl;
    }
    int main ()
    {
    	string S1 = "DEF";
    	string CP1 = "ABC";
    	string CP2 = "DEF";
    	string CP3 = "DEFG";
    	string CP4 ="def";
    	cout << "S1= " << S1 << endl;
    	cout << "CP1 = " << CP1 <<endl;
    	cout << "CP2 = " << CP2 <<endl;
    	cout << "CP3 = " << CP3 <<endl;
    	cout << "CP4 = " << CP4 <<endl;
    	cout << "S1 <= CP1 returned ";
    	TrueOrFalse (S1 <=CP1);
    	cout << "S1 <= CP2 returned ";
    	TrueOrFalse (S1 <= CP2);
    	cout << "S1 <= CP3 returned ";
    	TrueOrFalse (S1 <= CP3);
    	cout << "CP1 <= S1 returned ";
    	TrueOrFalse (CP1 <= S1);
    	cout << "CP2 <= S1 returned ";
    	TrueOrFalse (CP2 <= S1);
    	cout << "CP4 <= S1 returned ";
    	TrueOrFalse (CP4 <= S1);
    	cin.get();
    	return 0;
    }

    程序运行结果为:

    S1= DEF
    CP1 = ABC
    CP2 = DEF
    CP3 = DEFG
    CP4 = def
    S1 <= CP1 returned False
    S1 <= CP2 returned True
    S1 <= CP3 returned True
    CP1 <= S1 returned True
    CP2 <= S1 returned True
    CP4 <= S1 returned False

    在使用时比较运算符时,对于参加比较的两个字符串,任一个字符串均不能为 NULL,否则程序会异常退出。

     


    C++ string中的find()函数

    1.string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos

    2. s.find_first_of(flag) ,s.find_last_of(flag) :返回子串出现在母串中的首次出现的位置,和最后一次出现的位置

    3.s.find("b",5):查找某一给定位置后的子串的位置

    4. while((position=s.find(flag,position))!=string::npos) :查找所有子串在母串中出现的位置

    5. s.rfind (flag):反向查找子串在母串中出现的位置


    C++ int转string的几种方法比较

    #include<string.h>
    string str=to_string(n);
    

     如果Dev C++运行时报错“ to_string' was not declared in this scope ”

  • 相关阅读:
    Nhibernate对应关系参数介绍
    jquery mobile 登陆后页面验证
    jquery mobile radio,checkbox button 样式设置
    jquery mobile button样式设置
    Ext 三级联动 及附值
    store操作
    Javascript中try finally的细微差别
    mysql entity framework生成画面崩溃
    PYTHON推导生成
    PYTHON修饰器
  • 原文地址:https://www.cnblogs.com/chrysanthemum/p/12222200.html
Copyright © 2020-2023  润新知