目录
#include<iostream>
using namespace std;
//容器-string构造函数
/*
string();//创建字符串 例如:string str;
string(const char* s);//使用字符串s初始化;
string(const string& str)//使用一个string对象初始化另外一个string对象
string(int n,char c)//使用n个字符c初始化
*/
void test01()
{
string s1;
const char* str = "hello world";
string s2(str);
cout << "s2 = " << s2 << endl;
string s3(s2);
cout << "s3 = " << s3 << endl;
string s4(10, 'a');
cout << "s4 = " << s4 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
#include<iostream>
using namespace std;
//容器-string赋值操作
/*
string& operator=(const char * s); //char* 类型字符串赋值给当前字符串
string& operator=(const string &s); //把字符串s赋值给当前字符串
string& operator=(char *s); //把字符s赋值给当前字符串
string& assign(const char * s); //char* 类型字符串赋值给当前字符串
string& assign(const char * s,int n); //char* 类型字符串前n个字符赋值给当前字符串
string& assign(const string &s); //把字符串s赋值给当前字符串
string& assign(int n,char c);//用n个字符c赋值给当前字符串
*/
void test01()
{
string str1;
str1 = "hello woeld";
cout << "str1 = " << str1 << endl;
string str2;
str2 = str1;
cout << "str2 = " << str2 << endl;
string str3;
str3 = 'a';
cout << "str3 = " << str3 << endl;
string str4;
str4.assign("hello c++");
cout << "str4 = " << str4 << endl;
string str5;
str5.assign("hello c++",5);
cout << "str5 = " << str5 << endl;
string str6;
str6.assign(str5);
cout << "str6 = " << str6 << endl;
string str7;
str7.assign(7, 'd');
cout << "str7 = " << str7 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
#include<iostream>
using namespace std;
//容器-string+字符串拼接
/*
string &operator+=(const char *str); //重载+=操作符
string &operator+=(const char c); //重载+=操作符
string &operator+=(const string &str); //重载+=操作符
string &append(const char *s); //把字符串s连接到当前字符串结尾
string &append(const char *s,int n); //把字符串s的前n个字符连接到当前字符串结尾
string &append(const string &s); //同operator+=(const string &str)
string &append(const string &s,int pos,int n); //字符串s中从pos开始的n个字符连接到字符串结尾
*/
void test01()
{
string str1 = "我";
str1 += "爱看";
cout << "str1 = " << str1 << endl;
str1 += ':';
cout << "str1 = " << str1 << endl;
string str2 = "My Brilliant Friend";
str1 += str2;
cout << "str1 = " << str1 << endl;
string str3 = "I";
str3.append(" Love Flower!");
cout << "str3 = " << str3 << endl;
str3.append("Lily 123", 5);
cout << "str3 = " << str3 << endl;
//str3.append(str2);
//cout << "str3 = " << str3 << endl;
str3.append(str2, 3, 9);//从位置3开始 往后年截取9个字符+
cout << "str3 = " << str3 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
查找:查找指定的字符串是否存在
替换:在指定的位置替换字符串
rfind和find区别:rfind从右往左找,find从左往右找
#include<iostream>
using namespace std;
//容器-string字符串查找和替换
void test01()
{
string str1 = "abcdefg";
int pos = str1.find("de");//返回3
//int pos = str1.find("df");//没有的话,返回-1
if (pos == -1)
{
cout << "未找到字符串!" << endl;
}
else
{
cout << "找到字符串,pos = " << pos << endl;
}
//rfind 和find的区别
//rfing从右往左找,find是从左往右
pos = str1.rfind("de");
cout << "rfing,pos = " << pos << endl;
}
//2、替换
void test02()
{
string str = "abcdefg";
str.replace(1, 3, "1111");
cout << "replace str = " << str << endl;
}
int main()
{
test02();
system("pause");
return 0;
} 
总结:通常比较两个字符是否相等
#include<iostream>
using namespace std;
//容器-string字符串比较
void test01()
{
string str1 = "hello";
string str2 = "xello";
if (str1.compare(str2) == 0)
{
cout << "str1 等于 str2" << endl;
}
else if(str1.compare(str2) > 0)
{
cout << "str1 小于 str2" << endl;
}
else
{
cout << "str1 大于 str2" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}

#include<iostream>
using namespace std;
//容器-string字符存取
void test01()
{
string str = "hello";
//1、通过[] 访问单个字符
for (int i = 0; i < str.size(); i++)
{
cout << str[i] << " ";
}
cout << endl;
//通过at 访问当个字符
for (int i = 0; i < str.size(); i++)
{
cout << str.at(i) << " ";
}
cout << endl;
str[0] = 'x';
cout << "str = " << str << endl;
str.at(1) = 'x';
cout << "str = " << str << endl;
}
int main()
{
test01();
system("pause");
return 0;
}

#include<iostream>
using namespace std;
//容器-string字符串插入和删除
void test01()
{
string str = "hello";
//插入
str.insert(1, "1111");
cout << "str = " << str << endl;
//删除
str.erase(1, 4);
cout << "str = " << str << endl;
string str2 = "World";
str.insert(str.size(), str2);
cout << "str = " << str << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
string substr(int pos = 0, int n=npos)const ;//返回由pos开的的n个字符组成的字符串
#include<iostream>
using namespace std;
//容器-string字符串插入和删除
void test01()
{
string str = "Hello World";
string subStr = str.substr(1, 3);
cout << "subStr = " << subStr << endl;
}
//实用操作
void test02()
{
string email = "lisi@sina.com";
//从邮件地址中 获取 用户名信息
int pos = email.find("@");
cout << pos << endl;
string userName = email.substr(0, pos);
cout << "User Name = " << userName << endl;
}
int main()
{
test02();
system("pause");
return 0;
}


注意:

#include<iostream>
#include <vector>
using namespace std;
//打印
void printVector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int>v1;//默认构造 无参构造
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
//通过区间的方式进行构造
vector<int>v2(v1.begin(), v1.end());
printVector(v2);
//n个elem
vector<int>v3(10, 100);//10个100
printVector(v3);
//拷贝构造
vector<int>v4(v3);
printVector(v4);
}
int main()
{
test01();
system("pause");
return 0;
}

#include<iostream>
#include <vector>
using namespace std;
void printVector(vector<int> &v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//vector赋值
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
//赋值=
vector<int>v2 = v1;
printVector(v2);
//assign
vector<int>v3;
v3.assign(v1.begin(), v1.end());
printVector(v3);
//n个elem方式赋值
vector<int>v4;
v4.assign(10, 100);
printVector(v4);
}
int main()
{
test01();
system("pause");
return 0;
}

#include<iostream>
#include <vector>
using namespace std;
void printVector(vector<int>v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//vector容量和大小
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
if (v1.empty())//为真,代表容器为空,即v1为空
{
cout << "v1为空" << endl;
}
else
{
cout << "v1 不为空" << endl;
cout << "v1的容量:" << v1.capacity() << endl;
cout << "v1的大小:" << v1.size() << endl;
}
//重新指定大小
//v1.resize(15);
//printVector(v1);//如果重写指定的长度比原来长,默认用0填充
v1.resize(15,100);//利用重载版本,可以指定默认值填充,参数2
printVector(v1);
v1.resize(5);//如果重写指定的长度比原来短,超出部分被删除
printVector(v1);
}
int main()
{
test01();
system("pause");
return 0;
}
#include<iostream>
#include <vector>
using namespace std;
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//vector插入和删除
void test01()
{
vector<int>v;
//尾插
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
printVector(v);
//尾删
v.pop_back();
printVector(v);
//插入
v.insert(v.begin(), 100);//第一个参数是迭代器
printVector(v);
v.insert(v.begin(), 2, 1000);//第一个参数是迭代器
printVector(v);
//删除
v.erase(v.begin());//也是迭代器
printVector(v);
//类似于清空
//v.erase(v.begin(),v.end());//也是迭代器
v.clear();
printVector(v);
}
int main()
{
test01();
system("pause");
return 0;
}

#include<iostream>
#include <vector>
using namespace std;
//vector数据存取
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//利用[]访问数组中的元素
for (int i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
//利用at方式访问元素
for (int i = 0; i < v.size(); i++)
{
cout << v.at(i) << " ";
}
cout << endl;
//获取第一个元素
cout << "第一个元素:" << v.front() << endl;
//获取最后一个元素
cout << "最后一个元素:" << v.back() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
函数原型:swap(vec);//将vec与本身的元素互换
#include<iostream>
#include <vector>
using namespace std;
//vector互换
void printVector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//1、基本使用
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
cout<<"交换前:" << endl;
printVector(v1);
vector<int>v2;
for (int i = 10; i > 0; i--)
{
v2.push_back(i);
}
printVector(v2);
//交换后
cout<<"交换后:" << endl;
v1.swap(v2);
printVector(v1);
printVector(v2);
}
//2、实际使用
//巧用swap可以收缩内存空间
void test02()
{
vector<int>v;
for (int i = 0; i < 10000; i++)
{
v.push_back(i);
}
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
v.resize(3);
cout << "resize v的容量为:" << v.capacity() << endl;
cout << "resize v的大小为:" << v.size() << endl;
vector<int>(v).swap(v); //vector<int>(v)是匿名对象 系统在执行完成该句之后 释放
cout << "swap v的容量为:" << v.capacity() << endl;
cout << "swap v的大小为:" << v.size() << endl;
}
int main()
{
test02();
system("pause");
return 0;
}
sawp收缩内存的原因:匿名对象在执行完后,系统自动释放

功能描述:减少vector在动态扩展容量时的扩展次数
函数原型:reserve(int,len);//容器预留len个元素长度,预留位置不初始化,元素不可访问
#include<iostream>
#include <vector>
using namespace std;
//vector预留空间
void test01()
{
vector<int>v;
//利用reserve预留空间
v.reserve(100000);
int num = 0;
int* p = NULL;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
//每次开辟新内存 都会使得首地址变化
if (p != &v[0])
{
p = &v[0];
num++;
}
}
cout << "num = " << num << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
总结:如果数据量很大,可以一开始就利用resreve预留出空间



#include<iostream>
#include <deque>
using namespace std;
void printDeque(const deque<int>& d)//加const只读容器
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
//*it = 100;//加const只读容器 容器中的数据不可以修改
cout << *it << " ";
}
cout << endl;
}
//deque构造函数
void test01()
{
deque<int>d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printDeque(d1);
deque<int>d2(d1.begin(), d1.end());
printDeque(d2);
deque<int>d3(10, 100);
printDeque(d3);
deque<int>d4(d3);
printDeque(d4);
}
int main()
{
test01();
system("pause");
return 0;
}

#include<iostream>
#include <deque>
using namespace std;
void printDeque(deque<int>&d)
{
for (deque<int>::iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//deque赋值操作
void test01()
{
deque<int>d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printDeque(d1);
//operator= 赋值
deque<int>d2;
d2 = d1;
printDeque(d2);
//assign
deque<int>d3;
d3.assign(d1.begin(), d1.end());
printDeque(d3);
deque<int>d4(10, 100);
printDeque(d4);
}
int main()
{
test01();
system("pause");
return 0;
}

deque容器没有容量限制,可以无限扩展
#include<iostream>
#include <deque>
using namespace std;
void printDeque(deque<int>& d)
{
for (deque<int>::iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//deque大小操作
void test01()
{
deque<int>d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
printDeque(d1);
if (d1.empty())
{
cout << "d1为空" << endl;
}
else
{
cout << "d1不为空" << endl;
cout << "d1容量:" << d1.size()<<endl;
//deque没有容量的概念
}
d1.resize(15);
//printDeque(d1);
d1.resize(15, 1);
printDeque(d1);
d1.resize(5);
printDeque(d1);
}
int main()
{
test01();
system("pause");
return 0;
}

#include<iostream>
#include <deque>
using namespace std;
void printDeque(const deque<int>& d)//const只读迭代器
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//deque大小操作
void test01()
{
deque<int>d1;
//尾插
d1.push_back(10);
d1.push_back(20);
//头插 200 100 10 20
d1.push_front(100);
d1.push_front(200);
printDeque(d1);
//尾删
d1.pop_back();
//头删
d1.pop_front();
printDeque(d1);
}
//插入insert
void test02()
{
deque<int> d1;
d1.push_back(10);
d1.push_back(20);
d1.push_front(100);
d1.push_front(200);
//200 100 10 20
printDeque(d1);
d1.insert(d1.begin(),1000);
//1000 200 100 10 20
printDeque(d1);
d1.insert(d1.begin(), 2, 11);
//11 11 1000 200 100 10 20
printDeque(d1);
deque<int>d2;
d2.push_back(1);
d2.push_back(2);
d2.push_back(3);
d1.insert(d1.begin(), d2.begin(), d2.end());
//1 2 3 11 11 1000 200 100 10 20
printDeque(d1);
}
void test03()
{
deque<int> d1;
d1.push_back(10);
d1.push_back(20);
d1.push_front(100);
d1.push_front(200);
//删除
deque<int>::iterator it = d1.begin();
it++;
d1.erase(it);
printDeque(d1);
//区间方式删除
//类似于清空
//d1.erase(d1.begin(), d1.end());
//printDeque(d1);
//清空
d1.clear();
printDeque(d1);
}
int main()
{
test03();
system("pause");
return 0;
}

#include<iostream>
#include <deque>
using namespace std;
void printDeque(const deque<int>& d)//const只读迭代器
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//deque大小操作
void test01()
{
deque<int>d;
d.push_back(10);
d.push_back(20);
d.push_back(30);
d.push_front(100);
d.push_front(200);
d.push_front(300);
//通过[]方式访问元素
//300 200 100 10 20 30
for (int i = 0; i < d.size(); i++)
{
cout << d[i] << " ";
}
cout << endl;
//通过at的方式
for (int i = 0; i < d.size(); i++)
{
cout << d.at(i) << " ";
}
cout << endl;
cout << "第一个元素为:" << d.front() << endl;
cout << "最后一个元素为:" << d.back() << endl;
}
int main()
{
test01();
system("pause");
return 0;
}

#include<iostream>
#include <deque>
using namespace std;
#include<algorithm>
void printDeque(const deque<int>& d)//const只读迭代器
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//deque大小操作
void test01()
{
deque<int>d;
d.push_back(10);
d.push_back(20);
d.push_back(30);
d.push_front(100);
d.push_front(200);
d.push_front(300);
//300 200 100 10 20 30
printDeque(d);
//排序 默认排序规则 升序
//对于支持随机访问的迭代器,都可以利用sort算法之间进行排序
//vector容器也可以利用sort排序
sort(d.begin(), d.end());
cout << "排序后结果:" << endl;
printDeque(d);
}
int main()
{
test01();
system("pause");
return 0;
} 我想使用spawn(针对多个并发子进程)在Ruby中执行一个外部进程,并将标准输出或标准错误收集到一个字符串中,其方式类似于使用Python的子进程Popen.communicate()可以完成的操作。我尝试将:out/:err重定向到一个新的StringIO对象,但这会生成一个ArgumentError,并且临时重新定义$stdxxx会混淆子进程的输出。 最佳答案 如果你不喜欢popen,这是我的方法:r,w=IO.pipepid=Process.spawn(command,:out=>w,:err=>[:child,:out])
前言作为一名程序员,自己的本质工作就是做程序开发,那么程序开发的时候最直接的体现就是代码,检验一个程序员技术水平的一个核心环节就是开发时候的代码能力。众所周知,程序开发的水平提升是一个循序渐进的过程,每一位程序员都是从“菜鸟”变成“大神”的,所以程序员在程序开发过程中的代码能力也是根据平时开发中的业务实践来积累和提升的。提高代码能力核心要素程序员要想提高自身代码能力,尤其是新晋程序员的代码能力有很大的提升空间的时候,需要针对性的去提高自己的代码能力。提高代码能力其实有几个比较关键的点,只要把握住这些方面,就能很好的、快速的提高自己的一部分代码能力。1、多去阅读开源项目,如有机会可以亲自参与开源
我正在尝试找到一种方法来规范化字符串以将其作为文件名传递。到目前为止我有这个:my_string.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n,'').downcase.gsub(/[^a-z]/,'_')但第一个问题:-字符。我猜这个方法还有更多问题。我不控制名称,名称字符串可以有重音符、空格和特殊字符。我想删除所有这些,用相应的字母('é'=>'e')替换重音符号,并将其余的替换为'_'字符。名字是这样的:“Prélèvements-常规”“健康证”...我希望它们像一个没有空格/特殊字符的文件名:“prelevements_routin
电脑0x0000001A蓝屏错误怎么U盘重装系统教学分享。有用户电脑开机之后遇到了系统蓝屏的情况。系统蓝屏问题很多时候都是系统bug,只有通过重装系统来进行解决。那么蓝屏问题如何通过U盘重装新系统来解决呢?来看看以下的详细操作方法教学吧。 准备工作: 1、U盘一个(尽量使用8G以上的U盘)。 2、一台正常联网可使用的电脑。 3、ghost或ISO系统镜像文件(Win10系统下载_Win10专业版_windows10正式版下载-系统之家)。 4、在本页面下载U盘启动盘制作工具:系统之家U盘启动工具。 U盘启动盘制作步骤: 注意:制作期间,U盘会被格式化,因此U盘中的重要文件请注
目录前言滤波电路科普主要分类实际情况单位的概念常用评价参数函数型滤波器简单分析滤波电路构成低通滤波器RC低通滤波器RL低通滤波器高通滤波器RC高通滤波器RL高通滤波器部分摘自《LC滤波器设计与制作》,侵权删。前言最近需要学习放大电路和滤波电路,但是由于只在之前做音乐频谱分析仪的时候简单了解过一点点运放,所以也是相当从零开始学习了。滤波电路科普主要分类滤波器:主要是从不同频率的成分中提取出特定频率的信号。有源滤波器:由RC元件与运算放大器组成的滤波器。可滤除某一次或多次谐波,最普通易于采用的无源滤波器结构是将电感与电容串联,可对主要次谐波(3、5、7)构成低阻抗旁路。无源滤波器:无源滤波器,又称
最近在学习CAN,记录一下,也供大家参考交流。推荐几个我觉得很好的CAN学习,本文也是在看了他们的好文之后做的笔记首先是瑞萨的CAN入门,真的通透;秀!靠这篇我竟然2天理解了CAN协议!实战STM32F4CAN!原文链接:https://blog.csdn.net/XiaoXiaoPengBo/article/details/116206252CAN详解(小白教程)原文链接:https://blog.csdn.net/xwwwj/article/details/105372234一篇易懂的CAN通讯协议指南1一篇易懂的CAN通讯协议指南1-知乎(zhihu.com)视频推荐CAN总线个人知识总
深度学习部署:Windows安装pycocotools报错解决方法1.pycocotools库的简介2.pycocotools安装的坑3.解决办法更多Ai资讯:公主号AiCharm本系列是作者在跑一些深度学习实例时,遇到的各种各样的问题及解决办法,希望能够帮助到大家。ERROR:Commanderroredoutwithexitstatus1:'D:\Anaconda3\python.exe'-u-c'importsys,setuptools,tokenize;sys.argv[0]='"'"'C:\\Users\\46653\\AppData\\Local\\Temp\\pip-instal
我完全不是程序员,正在学习使用Ruby和Rails框架进行编程。我目前正在使用Ruby1.8.7和Rails3.0.3,但我想知道我是否应该升级到Ruby1.9,因为我真的没有任何升级的“遗留”成本。缺点是什么?我是否会遇到与普通gem的兼容性问题,或者甚至其他我不太了解甚至无法预料的问题? 最佳答案 你应该升级。不要坚持从1.8.7开始。如果您发现不支持1.9.2的gem,请避免使用它们(因为它们很可能不被维护)。如果您对gem是否兼容1.9.2有任何疑问,您可以在以下位置查看:http://www.railsplugins.or
我经常迷上ruby的一件事是递归模式。例如,假设我有一个数组,它可能包含无限深度的数组作为元素。所以,例如:my_array=[1,[2,3,[4,5,[6,7]]]]我想创建一个方法,可以将数组展平为[1,2,3,4,5,6,7]。我知道.flatten可以完成这项工作,但这个问题是作为我经常遇到的递归问题的一个例子-因此我试图找到一个更可重用的解决方案。简而言之-我猜这种事情有一个标准模式,但我想不出任何特别优雅的东西。任何想法表示赞赏 最佳答案 递归是一种方法,它不依赖于语言。您在编写算法时要考虑两种情况:再次调用函数的情
我正在使用ruby标准记录器,我想要每天轮换一次,所以在我的代码中我有:Logger.new("#{$ROOT_PATH}/log/errors.log",'daily')它运行完美,但它创建了两个文件errors.log.20130217和errors.log.20130217.1。如何强制它每天只创建一个文件? 最佳答案 您的代码对于长时间运行的应用程序是正确的。发生的事情是您在给定的一天多次运行代码。第一次运行时,Ruby会创建一个日志文件“errors.log”。当日期改变时,Ruby将文件重命名为“errors.log