C++string类常见用法(二)
来源:互联网 发布:商品标签软件打印机 编辑:程序博客网 时间:2024/05/23 18:32
同一部分,完全以代码形式作解
#include<string>#include<iostream>using namespace std;int main(){ string str= "hello "; const char *c = "RALPHFJY "; str.append(c); cout<<str<<endl; const char*ch = "and FUNKYA blablabla"; str.append(ch,10); cout<<str<<endl; string str1 = " and "; string str2 = "forever"; str.append(str1); str+=str2; cout<<str<<endl; string str3 = "@@@!#"; str.append(str3.begin()+3,str3.begin()+4); cout<<str<<endl; str.append(7,'h'); cout<<str<<endl;} //append的用法
#include<string>#include<iostream>using namespace std;int main(){ string str1 ="hello"; string str2 = "world"; str1.swap(str2); cout<<str1<<endl; cout<<str2<<endl; }
#include <iostream> // std::cout #include <string> // std::string int main () { std::string str ("There are two needles in this haystack with needles."); std::string str2 ("needle"); // different member versions of find in the same order as above: std::size_t found = str.find(str2); if (found!=std::string::npos) std::cout << "first 'needle' found at: " << found << '\n'; found=str.find("needles are small",found+1,6); if (found!=std::string::npos) std::cout << "second 'needle' found at: " << found << '\n'; found=str.find("haystack"); if (found!=std::string::npos) std::cout << "'haystack' also found at: " << found << '\n'; found=str.find('.'); if (found!=std::string::npos) std::cout << "Period found at: " << found << '\n'; // let's replace the first needle: str.replace(str.find(str2),str2.length(),"preposition"); //replace 用法 std::cout << str << '\n'; return 0; }
第三处代码来源于网络上的例子
#include<iostream>#include<string>using namespace std;int main(){ string str ="-45"; int signal = stoi(str); cout<<signal<<endl; string str2 = "-1.234"; double signal2 = stof(str2); cout<<signal2+3<<endl; int num = 123; string str3 = to_string(num); cout<<str3[1]<<endl;}
0 0
- C++string类常见用法(二)
- C++string类常见用法(一)
- string类常见函数汇总(二)
- C++ string类用法(转载)
- java常见类(二)String,StringBuffer,StringBuilder
- java中String类的常见用法
- String类常见用法的实例
- string 常见用法
- string类型常见用法
- string 的常见用法
- c++vector类常见用法
- C++string类用法总结
- c++string类 append用法
- linux常见命令用法之(二)
- 标准表达式-Thymeleaf常见用法(二)
- Date、String.format常见用法
- [STL] string的常见用法
- C++中的string用法(二)
- CF777B:Game of Credit Cards(贪心)
- Error:Execution failed for task ':app:mergeDebugResources'. > Error: Java.util.concurrent.ExecutionE
- 前端开发的模块化和组件化的定义,以及两者的关系?
- C#109课的主要内容
- keras学习笔记(一)
- C++string类常见用法(二)
- GYM 100247 F. Battle Fury(二分)
- Eclipse在线安装插件spket(可以自动提示extjs,支持ext4.0.7版本)
- PHP加入购物车接口的步骤
- radio、checkbox单击行,自动选中
- 数据库设计流程
- 高薪VS期权(CSDN观点活动投稿)
- Logistic函数
- SQL中的存储过程和函数