size_type find()
于字符串中寻找字符
#include <string>
#include <iostream>
using namespace std;
int main()
{
int n;
string const s = "This is a string";
// 从 string 开始搜索
n = s.find("is");//2
// 从位置 5 开始搜索
n = s.find("is", 5);//5
// 寻找单个字符
n = s.find('a');//8
}
rfind()
寻找子串的最后一次出现
#include <string>
#include <iostream>
using namespace std;
int main()
{
int n;
string const s = "This is a string";
// 从 string 开始搜索
n = s.rfind("is");//5
// 从位置 7 反向搜索
n = s.rfind("i", 7);//5
// 寻找单个字符
n = s.rfind('i');//13
}
stoi()/stol
string - >int/long
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "45";
string str2 = "3.14159";
int myint1 = std::stoi(str1);
int myint2 = std::stoi(str2);
cout << myint1 <<" "<< myint2 << endl;
return 0;
}
to_string()
int | float | double ->string
#include <iostream>
#include <string>
using namespace std;
int main()
{
int int1 = 45;
double d2 = 3.14159;
string mystr1 = to_string(int1);
string mystr2 = to_string(d2);
cout << mystr1 <<" "<< mystr2 << endl;
return 0;
}