string是一个类,char*是一个指向字符的指针; string封装了char*,管理字符串,是一个char*型的容器; string用于管理char*所分配的内存,不用考虑内存释放和越界; string提供一些字符串函数,如find、copy、erase、replace、insert; string构造函数 默认构造函数:string();用于构造一个空的字符串,如string s1; 拷贝构造...
string s3(s2); // 作用同上 string s4 = "hello world"; // 用 "hello world" 初始化 s4,除了最后的空字符外其他都拷贝到s4中 string s5("hello world"); // 作用同上 string s6(6,'a'); // 初始化s6为:aaaaaa string s7(s6, 3); // s7 是从 s6 的下标 3 开始的字符拷贝 string s8(s...
string &append(int n,char c); //在当前字符串结尾添加n个字符c string &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾 string的比较: bool perator==(const string &s1,const string &s2)const;//比较两个字符串是否相等 运算符">","<","...
char tolower(char ch ); char toupper(char ch ); 功能: 将大写字母转化成小写字母,将小写字母转换成大写字母 三、C++ string类相关操作 对于C++的string类来说,库函数定义了一系列的成员函数供我们使用,使用C++的string类来构建字符串,应包含头文件: #include <string>,并声明命名空间: using namespace std;...
串会覆盖老的字符串.使用c_str()打印的时候也是新的char*成员指向的地址.这样也就解释了在使用append对实例赋值时,后面打印的结果是追加字符串的值. 其实,前面说的都是废话,用一句话就可以说明,就是:在使用string中的c_str()进行字符串赋值时,如果后面对string中的char*进行改变.那么c_str()中的值就不在可...
因为getline函数返回时丢弃换行符,换行符将不会存储在string对象中。 Prototype: ssize_t getline (char **lineptr, size_t *n, FILE *stream) Description: This function reads an entire line from stream, storing the text (including the newline and a terminating null character) in a buffer and stor...
int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分 例如: #include <iostream> #include<string> using namespace std; int main() { string s = "Student"; string s1; cout <<"s1的容量为:"<<s1.capacity()<< endl; ...
// C program to Append a Character to a String #include <stdio.h> #include <string.h> int main() { // declare and initialize string char str[6] = "Geek"; // declare and initialize char char ch = 's'; // print string printf("Original String: %s\n", str); printf("Character...
#include<iostream>#include<string>using namespace std;//20200425 测试字符串操作 公众号:C与C语言plusintmain(){strings("hello");strings2("abc");s.insert(0,3,'A');//在s下标是0之前插入3个Acout<<s<<endl;//s为AAAhellos.insert(5,s2);//在AAAhello下标是5的元素之前插入abccout<<s<<endl...
//整数型字面量:long l=1L;int i=1;//浮点类型字面量:float f=11.1f;double d=11.1;//字符和字符串类型字面量:char c='h';String s="Hydra";//布尔类型字面量:boolean b=true;当我们在代码中定义并初始化一个字符串对象后,程序会在常量池(constant pool)中缓存该字符串的字面量,如果...