🆗,那这里呢其实跟一些历史原因有关,string呢其实出现的比STL早,string其实严格来说是不属于STL的,它是C++标准库产生的,在STL出现之前就已经在标准库出现了。 那string呢其实最早之前设计的就是length,因为字符串的长度嘛,用length就很合适。但是后面STL出现之后,里面的其它数据结构用的都是size,那为了保持一致,就...
intmain(){stringstr1("hello world");stringstr2(str1);//拷贝构造str1return0;} 2.5 string(const string& str,size_t pos,size_t len = npos) 代码语言:cpp 代码运行次数:0 运行 AI代码解释 intmain(){stringstr1("helloo world");stringstr2(str1,5,6);cout<<str2<<endl;return0;} 功能:...
🆗,那这里呢其实跟一些历史原因有关,string呢其实出现的比STL早,string其实严格来说是不属于STL的,它是C++标准库产生的,在STL出现之前就已经在标准库出现了。 那string呢其实最早之前设计的就是length,因为字符串的长度嘛,用length就很合适。但是后面STL出现之后,里面的其它数据结构用的都是size,那为了保持一致,就...
假设现在有一个文件名的字符串,比如说string.cpp,我们想把后缀名取出来,怎么搞? 这时我们就可以考虑使用substr这个成员函数。 它的作用是什么呢? 它可以帮助我们获取string对象中指定的一个子串。 参数: 那有了substr,我们就可以怎么解决上面的问题: 我们是不是可以先用find找的.的位置,然后计算出后缀名的长度len...
string file("string.cpp");size_t pos = file.rfind('.');string suffix(file.substr(pos, file.size() - pos));cout << suffix << endl;// npos是string里面的一个静态成员变量// static const size_t npos = -1;// 取出url中的域名string url("http://www.cplusplus.com/reference/string/...
[cpp]view plain copy print ? AI检测代码解析 1. #include <iostream> 2. #include <string> 3. using namespace std; 4. int main() 5. { 6. string str1; 7. "the size of "); 8. " hello world ";//空格不会忽略 9. str3+=str2; ...
//=== #include <iostream> #include <string> using namespace std; int main() { string s = "fsafdsfa"; cout << s.c_str() << endl; // 想要输出string类型的变量,不能直接输出要用s。c_str()这个方法 char chA = s[5]; char chD = s.at(5); cout <<"chA = "<<chA<<"\nchD...
string (basic_string<char>) 对与元组 pair tuple 2.2 向量 vector #include <vector> 连续的顺序的储存结构(和数组一样的类别),但是有长度可变的特性。 2.2.1 常用方法 构造 vector<类型> arr(长度, [初值]) 时间复杂度:$O(n)$ 常用的一维和二维数组构造示例,高维也是一样的(就是会有点长). vector...
注意string操作是对大小敏感的 常用的string搜索函数:搜索操作返回的都是下标,没有找到返回的是-1 s.find(args) 查找s中args第一次出现的位置 s.rfind(args) 查找s中args最后一次出现的位置 s.find_first_of(args) 在s中查找args中任何一个字符第一次出现的位置 s.find_last_of(args) 在s中查找args中任何...
string a; cin>>a; cout<<"你输出的信息为: "<<a; } int main() { cinAndCout(); return 0; } image-20220828220137609 Fstream iostream是可以从键盘获取输入信息;而fstream则是可以从TXT等文件中获得信息的。必须要包含下面的两个头文件才行。