线性容器vector #include<vector>
- 对于线性容器,除了front,back,其他的都用的地址进行操作。
初始化
vector<int> abc(10); //初始化了10个默认值为0的元素
vector<int> cde(10,1); //初始化了10个值为1的元素
- 通过数组地址初始化
int a[5] = {1,2,3,4,5};
vector<int> b(a, a+5);//通过数组a的地址初始化,注意地址是从0到5(左闭右开区间)
或 vector<int> res={-1,-1};
基础操作
排序
默认升序 std::sort(起点地址, 终点地址);
数组int a[] = {2, 1, 3};
排序 std::sort(a, a+3);
vector<int> a;
排序 std::sort(a.begin(), a.end());
放入函数指针 std::sort(起点地址, 终点地址,f)
;
简单降序
#include <iostream>
#include <vector>
#include<algorithm>
bool f(const int a, const int b) {
return a > b; // 降序排列
}
对vector<Obj> myobj ;
排序
bool f(const Obg& a, const Obg& b) {
return a.value < b.value; // True 不换
}
copy拷贝 copy(str_vec.begin(), str_vec.end(), new_vec.begin());
# https://www.delftstack.com/zh/howto/cpp/how-to-copy-an-array-in-cpp/#:~:text=%E4%BD%BF%E7%94%A8%20copy%20%28%29,%E5%87%BD%E6%95%B0%E5%9C%A8%20C%2B%2B%20%E4%B8%AD%E5%A4%8D%E5%88%B6%E4%B8%80%E4%B8%AA%E6%95%B0%E7%BB%84复制数组
#include <iostream>
#include <vector>
#include <iterator>
#include <string>
using std::cout; using std::endl;
using std::vector; using std::copy;
using std::string;
int main() {
vector<string> str_vec = { "Warty", "Hoary",
"Breezy", "Dapper",
"Edgy", "Feisty" };
vector<string> new_vec(str_vec.size());
copy(str_vec.begin(), str_vec.end(), new_vec.begin());
cout << "new_vec - | ";
copy(new_vec.begin(), new_vec.end(),
std::ostream_iterator<string>(cout," | "));
cout << endl;
return EXIT_SUCCESS;
}
max_element
int maxValue = *max_element(v.begin(),v.end());
c++ vector关于push_back()调用构造函数、析构函数的分析
队列 queue (vector minus?,注意pop()弹出栈顶元素void返回值)
queue<int> q;
q.front();q.back();// 没了begin(),end()的指针操作
q.push(x); q.pop(); q.empty();//三件套
栈 stack(queue --)
stack <int> s;
s.top();//只有一个取值端
s.push(x); s.pop(); s.empty();//三件套
关联容器和顺序容器
- 关联容器中的元素是按
关键字
来保存和访问的。 - 顺序容器中的元素是按它们在容器中的
位置
来顺序保存和访问的。
集合set(vector ++)
与线性容器vector不同,set<int> s;
主要集中于元素的操作。
s.insert
(x) s.erase(x)
s.find(x) set<int>::iterator it; it = st.find(x);
s.count(x)
返回0或1
s.empty() s.size()读大小还是用这个万能的函数
- 像数组一样 set也有begin()和end()
map(更复杂一点,迭代器返回的是一个class的地址)
初始化
- 增
std::map<int ,std::string> user;
user[0] = "a";
user[0] = "abc";//会覆写
map::iterator it = user.begin();//输出用 it->first ,it->second
auto it = user.begin();
- 查
using namespace std;
map<int,string>::iterator it; it = user.find(x);
if (it != user.end()){ user.erase(it);}
- 删
user.erase(x);
unordered_map ?
map是有序的,键保证能比较大小,红黑树实现,占用空间高,适用于有序的结构。unordered_map 无序,哈希表实现,查找效率高。
- 增
tppedef bool (*foo)(const Obj& a, const Obj& b);//定义函数指针类型foo
bool f(const Obg& a, const Obg& b) {
return a.value < b.value;
}
std::map<Obj ,std::string, foo> user(f);
user[0] = "a";//会覆写
LIST
String
#include<string>
- 例如
stack<string> S;
- atoi()和stoi()的区别----数字字符串的处理
size() 和 length()
-
size_t size() const;返回字符串的长度 返回字符串中字符数的计数。string::length 是 string::size 的别名,返回完全相同的值。http://www.cplusplus.com/reference/string/string/size/
-
string的方法 : https://cplusplus.com/reference/string/string/
str.substr (3,5)
// string::substr https://cplusplus.com/reference/string/string/substr/
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos); // get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}
s.find(“***”)
std::string str="<folder:name> </folder>";
std::size_t pos = str.find("<folder");
if (pos != string::npos){
***
//string::npos可以表示string的结束 ,是string::type_size 类型的,也就是find()返回的类型。find函数在找不到指定值得情况下会返回string::npos
// means "until the end of the string".
}
- C++ 字符串分割
#include <string>
#include <vector>
#include <sstream>
或者
#include<bits/stdc++.h>
istringstream iss(str);;// 输入流
vector<string> vec;
string tmp;// 接收缓冲区
while (getline(iss, tmp, ','))
{
vec.push_back(tmp);// 输出
}
或者
stringstream ss(str);// 输入流
vector<string> vec;
string tmp;// 接收缓冲区
while (getline(ss, tmp, ','))
{
vec.push_back(tmp);// 输出
cout<< tmp;
}
char* c;
string s="1234";
c = s.c_str();
//error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
应该使用 const char* c ;
c++ char转string
在C++中,你可以将一个char
转换为一个std::string
对象。有多种方法可以完成这个转换,以下是两种常见的方法:
- 使用构造函数:
char myChar = 'A';
std::string myString(1, myChar);
这里的std::string(1, myChar)
使用了std::string
的构造函数,第一个参数指定要重复的字符数量,第二个参数是要重复的字符。这将创建一个包含单个字符'A'
的字符串。
- 使用字符串拼接:
char myChar = 'A';
std::string myString = "";
myString += myChar;
在这种方法中,我们首先将一个空字符串赋值给myString
,然后使用+=
运算符将myChar
添加到字符串中。
这两种方法都可以将单个字符转换为一个字符串。选择哪种方法取决于你的编程上下文和偏好。
双端容器 double end queue
- deque maxq; //front(),back(),push_back(value),push_front(value),pop_back(),pop_front()
- vector类中为什么没有push_front方法和pop_front方法
参考与更多
注意iter是类的成员,而非对像的成员
- 为什么不用new创建vector
// 1.not delete[], Manually delete all the elements in the vector when the object is destroyed.
~bar::bar()
{
for(std::vector<my_obj*>::iterator loop = foo.begin(); loop != foo.end(); ++loop)
{
delete (*loop);
}
}
- The java.util.Stack.peek() method in Java is used to retrieve or fetch the first element of the Stack or the element present at the top of the Stack. The element retrieved does not get deleted or removed from the Stack.
- set用例
- MAP用例
- map.clear()
- 在 C++11 之后,vector 容器中添加了新的方法:emplace_back() ,和 push_back() 一样的是都是在容器末尾添加一个新的元素进去,不同的是 emplace_back() 在效率上相比较于 push_back() 有了一定的提升。
vector< vector<int> > arr(9, vector<int> (10) );
// 9行10列 二维数组- swap : https://en.cppreference.com/w/cpp/algorithm/swap
- what’s the bahaviro of pop_back for an empty vector? Undefined behaviour
- 【C++】万能头文件 <bits/stdc++.h> 的用法和优缺点
- 0xc0c0c0c0 = -1061109568; 0x3f3f3f3f = 1061109567