stl取出字符串中的字符_在C ++ STL中使用比较运算符比较两个字符串
stl取出字符串中的字符
字符串作為數據類型 (String as datatype)
In C, we know string basically a character array terminated by \0. Thus to operate with the string we define character array. But in C++, the standard library gives us the facility to use the string as a basic data type like an integer. Like integer comparisons, we can do the same for strings too.
在C語言中,我們知道字符串基本上是一個以\ 0結尾的字符數組。 因此,要對字符串進行操作,我們定義了字符數組。 但是在C ++中,標準庫為我們提供了將字符串用作基本數據類型(如整數)的便利。 像整數比較一樣,我們也可以對字符串進行相同的操作。
Example:
例:
Like we define and declare,int i=5, j=7;Same way, we can do for a string like,string s1="Include", s2="Help";Like integers (i==j) , (i>j), (i<j)We can also do the same likeif(s1==s2)cout << 'They are same\n";else if(s1>s2)cout<<s1<<" is greater\n";elsecout<<s2<< "s2 is greater\n";Remember, a string variable (literal) need to be defined under "". 'a' is a character whereas "a" is a string.
請記住,需要在“”下定義一個字符串變量(文字)。 “ a”是字符,而“ a”是字符串。
兩個字符串之間的比較如何工作? (How comparison between two string works?)
'==' operator
'=='運算符
Two strings need to be lexicographically same to return true which checking for == operator. "Include" and "Include" is the same. But, "INCLUDE" and "Include" is not same, i.e., case matters.
兩個字符串在字典上必須相同才能返回true,這將檢查==運算符。 “包含”和“包含”相同。 但是, “ INCLUDE”和“ Include”不相同,即大小寫有關。
'>' , '
'>','
This two operator checks which string is greater(smaller) lexicographically. The checking starts from the initial character & checking is done as per ascii value. The checking continues until it faces the same character from both strings. Like,
這兩個運算符在字典上檢查哪個字符串更大(或更小)。 從初始字符開始檢查,并根據ascii值進行檢查。 繼續檢查,直到面對兩個字符串中的相同字符。 喜歡,
"Include" > "Help" as 'I' > 'H'"Include" < "India" as 'c' < 'd'Header file needed:
所需的頭文件:
#include <string>Or#include <bits/stdc++.h>C++ program to compare two strings using comparison operator (==)
C ++程序使用比較運算符(==)比較兩個字符串
#include <bits/stdc++.h> using namespace std;void compare(string a, string b){if(a==b)cout<<"strings are equal\n";else if(a<b)cout<<b<<" is lexicografically greater\n";elsecout<<a<<" is lexicografically greater\n"; }int main(){string s1,s2;cout<<"enter string1\n";cin>>s1;cout<<"enter string2\n";cin>>s2;compare(s1,s2); //user-defined function to comaprereturn 0; }Output
輸出量
First run: enter string1 Include enter string2 Help Include is lexicografically greaterSecond run: enter string1 Include enter string2 India India is lexicografically greater翻譯自: https://www.includehelp.com/stl/comparing-two-string-using-comparison-operators-in-cpp-stl.aspx
stl取出字符串中的字符
總結
以上是生活随笔為你收集整理的stl取出字符串中的字符_在C ++ STL中使用比较运算符比较两个字符串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在Java中从字符串转换为双精度
- 下一篇: Spring Cloud Alibaba