string 字串連線

2021-08-04 16:53:27 字數 711 閱讀 5536

有了 string 類,我們可以使用」

+「或」

+=「運算子來直接拼接字串,非常方便,再也不需要使用

c語言中的

strcat()

、strcpy()

、malloc()

等函式來拼接字串了,再也不用擔心空間不夠會溢位了。

用」+「來拼接字串時,運算子的兩邊可以都是

string

字串,也可以是乙個

string

字串和乙個

c風格的字串,還可以是乙個

string

字串和乙個

char

字元。

請看下面的例子:

#include

#include

using namespace std;

int main(){

string s1, s2, s3;

s1 = "first";

s2 = "second";

s3 = s1 + s2;

cout<< s3 cout<< s2 cout<< s1 cout<< s1 執行結果:

firstsecond

secondfirst

firstthird

firstthirda

例如string a = 「123」;

string b = 「456」;

string 字串連線

有了 string 類,我們可以使用 或 運算子來直接拼接字串,非常方便,再也不需要使用 c語言中的 strcat strcpy malloc 等函式來拼接字串了,再也不用擔心空間不夠會溢位了。用 來拼接字串時,運算子的兩邊可以都是 string 字串,也可以是乙個 string 字串和乙個 c風格...

字串連線

輸入n個字串s i 你要把他們按某個順序連線起來,使得字典序最小。1 n 100 每個字串長度 100 字串只包含小寫字母 input 第一行乙個整數n。接下來每行乙個字串s i output 一行乙個字串表示把輸入的n個字串按某個順序連線之後的結果input示例 6 itlooks like an...

字串連線

mysql select abc 123 abc 123 123 1 row in set,1 warning 0.00 sec mysql select 123 123 123 123 246 1 row in set 0.00 sec mysql select 123 123 123 123 2...