C++的字符串
2022/1/31 1:04:28
本文主要是介绍C++的字符串,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include "project4.h" #include <cstdio>//stdio.h #include <iostream> #include <vector> #include <string> using namespace std; int main() { //int v[]{ 12,13,14,15,16,18 }; //for (auto x:v) //把v的每一个值考入x中 //{ // cout << x << endl; //} //cout << "-----------------------" << endl; //for (auto x : {1,2,3,4,65,77,88}) //{ // cout << x << endl; //} //cout << "-----------------------" << endl; //for (auto &x : v) //x就相当于v 引用型 指针 //{ // cout << x << endl; //} //char str[100] = "i love you";//c语言方法 //string s1; //默认初始化 //string s2 = "i love you"; //string s3 = ("i love you"); //string s4 = s2; //int num = 6; //string s5(num, 'a');//6个 'a' // //cout << s5.length() << endl; //cout << s5.size() << endl; //cout << s3[5] << endl; vector<int> intList; //C# 中的泛型 vector<string> stringList; vector<string> stringList2(stringList);//拷贝 vector<string> stringList3 = stringList;//拷贝 vector<string> stringList4 = { "abc","dce" };//初始化 vector<int> str1(15, 200); //创建15个200的字符串 vector<string> str2(10); vector<string> str3{ "10" }; stringList.push_back("abcde"); stringList.push_back("fghij"); stringList.push_back("klmn"); for (size_t i = 0; i < str1.size(); i++) { cout << str1[i] << endl; } cout << "--------------------------------" << endl; for (auto &str1 :str1) { str1 *= 2; //所有元素乘以2 } cout << "--------------------------------" << endl; for (auto str1 : str1) { cout << str1 << endl; //相当于for each str1 *=2会报错 } cout << "--------------------------------" << endl; //for (auto str1 : str1) //{ // str1.push_back(200); //会报错 // cout << str1 << endl; //} //for (size_t i = 0; i < stringList.size(); i++) //{ // cout << stringList[i] << endl; //abcde fghij klmn //} vector<string> stringList2(stringList);//拷贝 vector<string> stringList3 = stringList;//拷贝 //C++中 内存详细分为5个区域: //(1)栈 一般是函数内部的局部变量,动态存储区,由编译器自动存储/分配和释放 //(2)堆 程序员malloc/new 分配,用free/delete释放。忘记释放后,系统会自动回收 // (3) 全局/静态变量:放全局/静态变量 static。 程序结束后系统释放 // (4) 常量存储区 // (5) 程序代码区 //栈和堆的区别 //(1) 栈 空间 有限。 这是系统int a = 4; 分配速度快,程序员控制不了。 //(2) 堆 :只要不超出实际物理内存,在操作系统允许分配的最大内存大小之内,都可以进行分配。 //在c中用malloc和free()分配和释放,这两者是函数。memory allocation:动态内存分配 }
这篇关于C++的字符串的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-28pyqt 怎么打包整个项目-icode9专业技术文章分享
- 2024-09-28laravel Commands 创建带有参数的 Artisan 命令的步骤和示例-icode9专业技术文章分享
- 2024-09-28antd怎么实现渲染tiff图片-icode9专业技术文章分享
- 2024-09-28英文半角中划线和中文全角的中划线有什么区别-icode9专业技术文章分享
- 2024-09-28nvm npm 和node 他们之间有什么关系-icode9专业技术文章分享
- 2024-09-28Node Version Manager (nvm)使用教程-icode9专业技术文章分享
- 2024-09-28nvm命令太慢,是什么原因-icode9专业技术文章分享
- 2024-09-28Kotlin 如何增加、删除和修改 MutableStateFlow 中的值。-icode9专业技术文章分享
- 2024-09-28Kotlin的stateFlow.update 写法介绍-icode9专业技术文章分享
- 2024-09-28kotlin 怎么获取当前时间格式-icode9专业技术文章分享