#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <bitset>
#include <cassert>
using namespace std;
void use_string(){
// 初始化
string s1 = "abc";
string s2;
cout << "string s1:" << s1 << endl;
// 常用操作
cout << "string 输入: cin >> s1;" << endl;
cout << "输出string: cout << s1 << endl;" << endl;
cout << "读入一行string: getline(cin, s1);" << endl;
cout << "string s1.size():" << s1.size() << endl;
cout <<"字符 s1[1]:" << s1[1] << endl;
cout << "寻找字串: s1.find(s2), 下标: " << s1.find("b") << ",没找到就返回 -1 或 string::npos" << endl;
cout << "转c风格字符串 s1.c_str() :" << s1.c_str() << endl;
string s3[100];
sort(s3, s3+1);
cout << "substr(start, len), s1.substr(1,1) : " << s1.substr(1,1) << endl;
// 删除 string& erase (size_t pos = 0, size_t len = npos);
s1.erase(0,1);
cout << "删除首个字符后 s1: "<< s1 << endl;
// 插入 string& insert (size_t pos, const string& str);
s1.insert(0, "ef");
cout << "插入两个字符后 s1: " << s1 << endl;
}
void use_vector() {
vector<int> v = {1,2,3,4};
cout << "v.size():" << v.size() << endl;
cout << "v[2]:" << v[2] << endl;
v.push_back(5) ;
v.clear();
v.push_back(1);
v.push_back(2);
for(int i=0; i < (int)v.size();i++)
cout << v[i] << " ";
cout << endl;
for (vector<int>::iterator it = v.begin(); it !=v.end(); it++)
cout << *it << " ";
cout << endl;
}
void use_queue() {
queue<int> q;
q.push(10);
q.push(11);
q.push(12);
q.pop();
cout << "queue --------- start" << endl;
cout << q.front() << endl;
cout << q.back() << endl;
cout << "queue ---------- end\n" << endl;
cout << "p queue --------- start" << endl;
priority_queue<int> pq;
pq.push(10);
pq.push(13);
pq.push(9);
cout << pq.top() << endl;
cout << "p queue --------- end\n" << endl;
priority_queue<int, vector<int>, greater<int>> small_pq; //小根堆
cout << "small pq --- start " << endl;
small_pq.push(4);
small_pq.push(1);
small_pq.push(3);
cout << small_pq.top() << endl;
cout << "small pq ----- end\n" << endl;
struct Rec {
int a,b;
Rec(int _a, int _b): a(_a),b(_b) {}
// bool operator< (const Rec& t) const {
// return a < t.a;
// }
bool operator>(const Rec& t) const {
return a > t.a;
}
};
cout << "pq struct ------ start" << endl;
priority_queue<Rec,vector<Rec>, greater<Rec>> pq1;
pq1.push(Rec(101,2));
pq1.push(Rec(303,4));
Rec r1 = pq1.top();
cout << r1.a << endl;
cout << "pq struct ------ end\n" << endl;
}
void use_stack() {
stack<int> stack;
stack.push(21);
stack.push(22);
stack.push(24);
stack.push(25);
stack.pop();
stack.pop();
cout <<"stack -------- start " << endl;
while (!stack.empty()) {
cout << ' ' << stack.top();
stack.pop();
}
cout << endl;
cout <<"stack -------- end " << endl;
}
void use_deque() {
// vector + queue的结合
deque<int> d;
d.begin(),d.end();
d.front(),d.back();
d.push_back(10),d.pop_front();
d.push_front(11),d.pop_back();
d[0];
d.clear();
d.size();
}
void use_set() {
set<int> s;
multiset<int> b;
// set<int>::iterator it = s.begin();
// it++,it--;
int x = 100;
s.end();
s.insert(x);
if (s.find(x)!=s.end()) {
cout << "set 存在x" << endl;
}
s.lower_bound(x); // 大于等于x的最小值
s.upper_bound(x); // 大于X的最小值
// s.erase(x);
// s.erase(it);
s.count(x); // 个数
struct Rec {
int x, y;
bool operator< (const Rec& t) const {
return x<t.x;
}
};
unordered_set<int> s2;
// 没有lower_bound 和 upper_bound
}
void use_map() {
map<string, vector<int>> a;
a.clear();
a["wxx"] = vector<int>({1,2,3});
cout << a["wxx"].size() << endl;
a.begin(),a.end();
a.size(),a.empty();
cout << (a.find("wxx") == a.end()) << endl;
// multimap<int, int> b;
unordered_map<int, int> c1;
unordered_multimap<int, int> c2;
}
void use_bitset () {
bitset<4> b = {3};
cout << b << endl;
// constructors:
std::bitset<4> b1;
std::bitset<4> b2{0xA}; // == 0B1010
std::bitset<4> b3{"0011"}; // can't be constexpr yet
std::bitset<8> b4{"ABBA", /*length*/4, /*0:*/'A', /*1:*/'B'}; // == 0B0000'0110
// bitsets can be printed out to a stream:
std::cout << "b1:" << b1 << "; b2:" << b2 << "; b3:" << b3 << "; b4:" << b4 << '\n';
// bitset supports bitwise operations:
b3 |= 0b0100; assert(b3 == 0b0111);
b3 &= 0b0011; assert(b3 == 0b0011);
b3 ^= std::bitset<4>{0b1100}; assert(b3 == 0b1111);
// operations on the whole set:
b3.reset(); assert(b3 == 0);
b3.set(); assert(b3 == 0b1111);
assert(b3.all() && b3.any() && !b3.none());
b3.flip(); assert(b3 == 0);
// operations on individual bits:
b3.set(/* position = */ 1, true); assert(b3 == 0b0010);
b3.set(/* position = */ 1, false); assert(b3 == 0);
b3.flip(/* position = */ 2); assert(b3 == 0b0100);
b3.reset(/* position = */ 2); assert(b3 == 0);
// subscript operator[] is supported:
b3[2] = true; assert(true == b3[2]);
// other operations:
assert(b3.count() == 1);
assert(b3.size() == 4);
assert(b3.to_ullong() == 0b0100ULL);
assert(b3.to_string() == "0100");
bitset<5> a = 0b00100;
cout << a << endl;
cout << a.any() << endl;
cout << a.all() << endl;
a.set();
cout << a << endl;
a.reset();
a.set(1,true);
a.flip(0);
cout << a << endl;
cout << a[1] << endl;
a[0] = 0;
cout << a << endl;
}
int main() {
// use_string();
use_vector();
use_queue();
use_stack();
use_deque();
use_set();
use_map();
use_bitset();
return 0;
}
%%%
我也在写这个练习~
不过是可以动态输入的~
怎么动态输入啊
hhh
大佬改一下
这段代码……恕我直言
# CE
哦对了现在先指出一个:
priority_queue<int, vector<int>, greater<int>> small_pq; //小根堆
这里:
greater<int> >
要像这样加上空格,不然会# CE
好像不会有问题,用clang-format格式化了一下,风格就是不加空格的= =#