wordpress 视频站模版,百度指数数据来源,自己做的网站怎么上传文章,wordpress ez backup文章目录 一、C基础格式 1.打印hello, world 2.基本数据类型 二、string 1.string简介 2.string的声明和初始化 3.string其他基本操作 (1)获取字符串长度 (2) 拼接字符串( 或 append) (3#xff09;字符串查找#xff08;find#xff09; (4)字符串替换 (5)提取子字符串… 文章目录 一、C基础格式 1.打印hello, world 2.基本数据类型 二、string 1.string简介 2.string的声明和初始化 3.string其他基本操作 (1)获取字符串长度 (2) 拼接字符串( 或 append) (3字符串查找find (4)字符串替换 (5)提取子字符串(substr) (6)字符串比较(compare) (7)遍历string 循环枚举下标 三、输入输出 1.scanf和printf 2.cin和cout 3.取消同步流 一、C基础格式
1.打印hello, world
#include bits/stdc.h //万能头文件
using namespeace std;
int main()
{ cout hello, world endl; printf (hello, world); return 0;
}
2.基本数据类型
int a 1 //整数型
dobule b 3.14 //浮点型小数
char c A; //字符型
char d[] hello; //字符串
bool e 1(true) / 0(false); //布尔类型判断真假 二、string
1.string简介
string是C标准库的重要组成部分主要用于字符串处理。
使用string库需要在头文件中包括该库 #includestring string与char[]不同string实现了高度的封装可以很方便地完成各种字符串的操作比如拼接、截取、匹配等等。 (1)字符串管理:string封装了字符串的存储和管理。它自动处理字符串的内存分配和释放避免了手动管理内存的麻烦。
(2) 动态大小调整:string可以根据需要自动调整字符串的大小。在添加或删除字符时string会自动调整内部的存储容量确保足够的空间来容纳字符串。
(3)安全性: string提供了一些方法来确保字符串的安全性。例如它提供了越界访问检查以避 免访问超出字符串范围的字符。
(4) 迭代器支持:string支持迭代器可以使用迭代器遍历字符串中的字符进行字符级别的操作。
(5)兼容性: string是C标准库的一部分因此在C中广泛使用并且与其他标准库组件和 C语言特性兼容。
2.string的声明和初始化
#includeiosteram
#includestring
using namespeace std; int main()
{ string str1 //声明并初始化空字符串 string str2 hello world; //用字符串字面量用始化字符串
cout str1:str1endl;
cout str2:str2endl;
return 0;
}
3.string其他基本操作
(1)获取字符串长度 string str hello, world; int length str.length();//或者 int length str.size(); coutlengthendl
(2) 拼接字符串( 或 append) string str1 a; string str2 b; string result1 str1 str2; //使用 运算符 string result2 str1.append(, ).append(str2); //使用 append 函数 cout result 1 result1 endl; cout result 1 result1 endl;
(3字符串查找find string str hello, world; size_t pos str.find(world); if( pos ! string::npos)
{ cout Substring found at position: pos endl;
} else{ cout Substring not found. endl: }
(4)字符串替换 string str hello, world; replace(7, 5, a): cout Result: str endl;
(5)提取子字符串(substr) string str Hello, world!; string substrstr.substr(75); // 提取子字符串 cout Substring: subStr endl;
(6)字符串比较(compare)
字典序的比较方法是从小到大一个一个比较一旦遇到不相等的字符就确定大小关系。 string str1 Hello”; string str2 world ; int result str1.compare(str2);// 比较字符串 if(result 0) { cout strings are equal. endl;
} else if (result 0)
{ cout strings 1 is less than String 2. endl;
} else
{ cout strings 1 is greater than String 2. endl;
}
(7)遍历string 循环枚举下标 auto枚举(其中表示取引用类型如果对i修改将会改变原来的值)
string s Hello;
for(int i 0; i s.length(); i)
cout s[i];
cout \n;
for(auto i :s)
{ cout i;
ia; //此处的修改无效因为这个主是拷贝出来的而不是引用s的 }
cout \n; //此时s Hello for(auto i : s) {
cout i;
ia; //此处修改会改变s的字符值
} cout \n; //此时s aaaaa
cout s \n 三、输入输出 1.scanf和printf int main() {
int a, b;
scanf(%d %d,a, b);
printf(%d,%d\n,ab);
return 0;
} int main() {
doble a, b;
scanf(%lf %lf,a, b);
printf(%.2lf,%.3lf\n,ab); //自动四舍五入 .x保留位小数
return 0;
} int main() {
char c1, c2;
scanf(%c %c,c1, c2);
printf(%c %c,c1, c2);
return 0;
} int main() {
char s[10];
scanf(%s , s); //%s输入遇到空格或回车会停下
printf(%s, s);
return 0;
} int main() {
char s[15];
scanf(%^\n] , s); //^排除 \n回车
printf(%s, s);
return 0;
}
其中[]是一个正则表达式表示只要不是回车就读进去。
类型 对应标识符 int %d double %lf
char %c
char[] %s
scanf和sprintf的优势:
(1)格式化输入和输出
(2)效率高 2.cin和cout int main()
{ char s[10]; // cin输入字符串也是遇到空格或回车就结束
cin s;
cout s;
return 0;
}
3.取消同步流
由于cin和cout需要自动判断变量类型等内部原因当数据量较大时可能导致程序运行超时。
我们可以通过取消同步流来加速cin和cout加速后效率相差无几。
int main() {
ios::sync_with_stdio(e),cin.tie(e), cout.tie(e); //取消同步流 //其他操作不变 int x;cin x;
cout x \n; return 0;
}