C++ string assign()赋值常用方法
C++ string assign()賦值常用方法
函數assign()常用在給string類變量賦值.
常用方法有:
1,直接用另一個字符串賦值.
如str2.assign(str1);即用str1給str2賦值.
2,用另一個字符串的一個子串賦值
如str3.assign(str1, 2, 3);
3,用一個字符串的前一段子串賦值;
如str4.assign("World", 5);
4,用幾個相同的字符,賦值.
如str5.assign(10, 'c');
#include<iostream>
using namespace std;
//========================================
int main()
{
??? string str1("hello");
??? string str2;
??? string str3;
??? string str4;
??? string str5;
??? //====================================
??? str2.assign(str1);
??? str3.assign("World", 4);
??? str4.assign(str1, 2, 3);
??? str5.assign(10, 'c');
??? //====================================
??? cout<<str1<<endl;
??? cout<<str2<<endl;
??? cout<<str3<<endl;
??? cout<<str4<<endl;
??? cout<<str5<<endl;
??? //====================================
??? system("pause");
??? return 0;
}
//========================================
結果為:
hello
hello
Worl
llo
cccccccccc
總結
以上是生活随笔為你收集整理的C++ string assign()赋值常用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 银河麒麟系统服务端命令_麒麟系统介绍
- 下一篇: c++入门基础知识