string和C语言字符串之间的相互转换以及string常用函数
生活随笔
收集整理的這篇文章主要介紹了
string和C语言字符串之间的相互转换以及string常用函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stdio.h>using namespace std;/*** @brief string類的初始化* @note * @param argc: * @param *argv[]: * @retval */
void init_test(void)
{string s1 = "aaa";string s2("bbbb");string s3 = s2; //通過copy構造函數初始化string對象string s4(10, 'a');cout << "s1 " << s1 << endl;cout << "s2 " << s2 << endl;cout << "s3 " << s3 << endl;cout << "s4 " << s4 << endl;}/*** @brief 字符串的遍歷* @note * @param argc: * @param *argv[]: * @retval */
void list_string(void)
{cout << "list_string" << endl;string s1 = "abcdefg";//數組方式for (int i = 0; i < s1.length(); i++){//< 將字符串當成數組一樣使用cout << s1[i] << endl;}cout << "--------------------------------" << endl;//< 迭代器方式for (string::iterator it = s1.begin(); it != s1.end(); it ++)\{cout << (*it) << endl;}cout << "----------------------------------" << endl;//< at函數for (int i = 0; i < s1.length(); i ++){//< at函數能偶檢測異常,出現異常的時候可以拋出異常,異常可以用sty函數進行捕獲cout << s1.at(i) << endl; //< 拋出異常}cout << "---------------------" << endl;/* 異常測試 */try{//< 有異常的時候捕獲異常for (int i = 0; i < s1.length() + 3; i ++) //< 故意多出來三個字節用于拋出異常{cout << s1.at(i) << endl; //< 拋出異常}}catch(...) //< 捕捉所有的一場呢{std::cerr << "error exception" << '\n';}}/*** @brief 字符指針和string類型的轉換* @note * @param argc: * @param *argv[]: * @retval */
void charbuf_string(void)
{ cout << "charbuf_string" << endl;//< char * ==> stringstring s1 = "aaabcgdjsggkkws";//< string ==> char *const char *buf;buf = s1.c_str();printf("%s\n", s1.c_str());//< 3 s1的內容copy到buf中char buf1[128];s1.copy(buf1, 3, 0); //< 從零字符開始copy3個字符到buf1中cout << buf1 << endl;}
/*** @brief string cat* @note 字符串連接* @param argc: * @param *argv[]: * @retval */
void string_cat(void)
{//< string方式的連接cout << "string cat " << endl;string s1 = "aaa";string s2 = "bbb";string s3 = s1 + s2;cout << s3 << endl;cout << "-------------------" << endl;s2.append(s1);cout << s2 << endl;}/* * find函數 從pos位置開始查找c字符或者字符串的位置,函數找不到就返回 -1* rfind向前查找* * replace替換* swap交換* */
/*** @brief search string* @note 字符串的查找和替換* @param argc: * @param *argv[]: * @retval */
void search_string(void)
{string s1 = "wbm hello hajhdhhfd 111 name name name 3434654655";//< 第一次出現 name的位置int index = s1.find("name", 0);cout << "index = " <<index << endl;//< 求 name出現的次數,每一次出現的數組下標int offline = s1.find("name", 0);while (offline != -1){cout << "offline = " << offline << endl;offline = offline + 1;offline = s1.find("name", offline); }//< 替換成大寫offline = s1.find("name", 0);while (offline != string::npos){cout << "offline = " << offline << endl;s1.replace(offline, 1, "N");offline = offline + 1;offline = s1.find("name", offline); cout << "s1 替換之后" << s1 << endl; }
}/*** @brief main * @note string的簡單使用* @param argc: * @param *argv[]: * @retval */
int main(int argc, char const *argv[])
{init_test();list_string();charbuf_string();string_cat();search_string();cout << "hello world!" << endl;return 0;
}
總結
以上是生活随笔為你收集整理的string和C语言字符串之间的相互转换以及string常用函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 朱兴杰(1986-),男,泰康保险集团股
- 下一篇: SQLite-C语言实战