C++字符串类及其应用集2

我们在下面的集合1中讨论了字符串类及其一些函数。

null

C++字符串类及其应用集1

本文将讨论更多函数

字符串与字符数组 在C++中,除了字符数组外,还有一种类似的实现字符串的方式,即使用C++类标准库的一部分。要使用string类实现string,我们需要添加头文件 .字符数组和字符串之间的基本区别是,对于字符数组,必须在声明时分配大小,即所有分配的内存都是固定的,不能在运行时更改。然而,对于字符串,在声明时不需要指定大小和分配固定内存。

// C++ program to demonstrate Character Array
// and String
#include<iostream>
#include<string>// for string class
using namespace std;
int main()
{
// Size has to be predefined in character array
char str[80] = "GeeksforGeeks" ;
// Size not predefined in string
string s( "GeeksforGeeks" );
// Printing character array and string
cout << str << endl;
cout << s << endl;
return 0;
}


输出:

GeeksforGeeks
GeeksforGeeks

一些有用的字符串函数

  • 比较(字符串比较) :-用于比较两个字符串。它返回整数中第二个字符串和第一个字符串的差。

    // C++ program to demonstrate use of compare()
    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
    string str( "GeeksforGeeks" );
    string str1( "GeeksforGeeks" );
    // Comparing strings using compare()
    if ( str.compare(str1) == 0 )
    cout << "Strings are equal" ;
    else
    cout << "Strings are unequal" ;
    return 0;
    }

    
    

    输出:

    Strings are equal
  • 查找(“字符串”): 在字符串中搜索参数中指定的子字符串的第一个匹配项。它返回子字符串第一次出现的位置。
  • 首先查找(“字符串”): 在字符串中搜索与参数中指定的任何字符匹配的第一个字符。它返回匹配的第一个字符的位置。
  • 查找(“字符串”)的最后一个: 在字符串中搜索与参数中指定的任何字符匹配的最后一个字符。它返回匹配的最后一个字符的位置。
  • rfind(“字符串”): 在字符串中搜索参数中指定的子字符串的最后一次出现。它返回子字符串最后一次出现的位置

    // C++ program to demonstrate working of find(),
    // rfind(),find_first_of() and find_last_of()
    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
    string str( "The Geeks for Geeks" );
    // find() returns position to first
    // occurrence of substring "Geeks"
    // Prints 4
    cout << "First occurrence of "Geeks" starts from : " ;
    cout << str.find( "Geeks" ) << endl;
    // Prints position of first occurrence of
    // any character of "reef" (Prints 2)
    cout << "First occurrence of character from "reef" is at : " ;
    cout << str.find_first_of( "reef" ) << endl;
    // Prints position of last occurrence of
    // any character of "reef" (Prints 16)
    cout << "Last occurrence of character from "reef" is at : " ;
    cout << str.find_last_of( "reef" ) << endl;
    // rfind() returns position to last
    // occurrence of substring "Geeks"
    // Prints 14
    cout << "Last occurrence of "Geeks" starts from : " ;
    cout << str.rfind( "Geeks" ) << endl;
    return 0;
    }

    
    

    输出:

    First occurrence of "Geeks" starts from : 4
    First occurrence of character from "reef" is at : 2
    Last occurrence of character from "reef" is at : 16
    Last occurrence of "Geeks" starts from : 14
    
  • 插入(位置到开始,字符串到插入): 此函数用于在字符串中插入给定的子字符串。它有两个参数,第一个是插入子字符串的位置,第二个是子字符串。

    // C++ program to demonstrate working of insert()
    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
    string str( "Geeksfor" );
    // Printing the original string
    cout << str << endl;
    // Inserting "Geeks" at 8th index position
    str.insert(8, "Geeks" );
    // Printing the modified string
    // Prints "GeeksforGeeks"
    cout << str << endl;
    return 0;
    }

    
    

    输出:

    Geeksfor
    GeeksforGeeks
    
  • 清除(): 此函数用于清除字符串中的所有字符。执行此操作后,字符串变为空(长度变为0)。
  • 空() 测试字符串是否为空。此函数返回一个布尔值。

    // C++ program to demonstrate working of clear()
    // and empty()
    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
    string str( "GeeksforGeeks" );
    // clearing string
    str.clear();
    // Checking if string is empty
    (str.empty()==1)?
    cout << "String is empty" << endl:
    cout << "String is not empty" << endl;
    return 0;
    }

    
    

    输出:

    String is empty
    

本文由 曼吉特·辛格。 如果你喜欢Geeksforgek,并且想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享