日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

stl取出字符串中的字符_在C ++ STL中使用比较运算符比较两个字符串

發布時間:2025/3/11 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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中使用比较运算符比较两个字符串的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。