版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31456593/article/details/77887499
从头开始学算法:考研机试题练习(C/C++)–STL使用
最近重学C语言,刷的是胡凡写的《算法笔记》,这本书的题主要是面向考研机试和一般算法考试的,零基础入门,还不错,在此记录学习过程。
本文主要记录一些STL中容器和函数的用法。
#include <stdio.h>
#include <vector>
#include <set>
#include <string>
#include <iostream>
#include <map>
#include <queue>
#include <functional>
#include <stack>
using namespace std;
void use_vector()
{
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
printf("%d\n", v.size());
for (int i = 0; i < v.size(); i++)
printf("%d ", v[i]);
printf("\n");
//迭代器
vector<int>::iterator it;
v.pop_back();
v.pop_back();
for (it = v.begin(); it < v.end(); it++)
printf("%d ", *it);
printf("\n");
v.insert(v.begin() + 3, 66);
it = v.begin();
for (int i = 0; i < v.size(); i++)
printf("%d ", *(it + i));
printf("\n");
v.erase(v.begin() + 6, v.begin()+8);
for (int i = 0; i < v.size(); i++)
printf("%d ", *(it + i));
printf("\n");
v.clear();
v.push_back(0);
if (v.begin() == v.end() - 1) {
v.push_back(502);
it = v.begin();//clear()后it必须重新取
for (int i = 0; i < v.size(); i++)
printf("%d ", *(it + i));
printf("\n");
}
}
void use_set()
{
set<int> s;
for (int i = 0; i < 10; i++) {
s.insert(i);
}
s.insert(-23);
set<int>::iterator it;
for (it = s.begin(); it != s.end(); it++)
printf("%d ", *it);
printf("\n");
it = s.begin();
it++;
set<int>::iterator it2;
it2 = it;
it2++;
it2++;
s.erase(it, it2);
s.erase(s.find(7));
for (it = s.begin(); it != s.end(); it++)
printf("%d ", *it);
printf("\n");
s.clear();
printf("%d\n", s.size());
s.insert(66);
for (it = s.begin(); it != s.end(); it++)
printf("%d ", *it);
printf("\n");
}
void use_string()
{
string str;
cin >> str;
str = "hello"+str;
cout << str;
printf("\n%c\n", str[2]);
string::iterator it;
for (it = str.begin(); it < str.end(); it++) {
printf("%c", *it);
}
printf("\n");
str.erase(str.begin() + 5);
//str.erase(str[2]);
it = str.begin();
str.erase(it+str.find("fo"));
it = str.begin();
for (int i = 0; i < str.length(); i++) {
printf("%c", *(it + i));
}
printf("\n");
str.insert( 5, "606");
str.replace( 5, 2, "wwow");
cout << str.substr(0, str.size()) << endl;
cout << str.substr(2, 3) << endl;
if (string::npos == -1)
printf("-1 is true\n");
if (string::npos == 4294967295)
printf("4294967295 is true\n");
string str2;
str2.insert(str2.begin(), str.begin(), str.begin() + 4);
cout << str2<<endl;
if (str > str2)
cout << str << " big" << endl;
else
cout << str2 << " big" << endl;
}
string to_science_notation(string a, int precision)
{
int point_pos, integer_num, now_precision,zero_pos;
char power[100];
string res;
//消掉首部多余的0
zero_pos= a.find("0");
int index = 0;
//把前面连续的0都删掉
while (a[index] == '0') {
a.erase(index, 1);
}
//处理整数
point_pos = a.find(".");
if (point_pos == -1) {
//12=0.12*10^2
integer_num = a.size();
}
//处理小数
else {
integer_num = point_pos;
a.erase(point_pos, 1);
}
//处理小数点后面的0,得出负幂数
if (integer_num == 0) {
while (a[index] == '0') {
a.erase(index, 1);
integer_num--;
}
}
now_precision = a.size();
if (now_precision == 0)
integer_num = 0;
sprintf(power, "%d", integer_num);
if (now_precision >= precision) {
//现在精度大,截取前面的
res = "0." + a.substr(0, precision) + "*10^" + power;
}
else {
//现在精度小,后补0,10的幂均为整数位数
res = "0." + a.substr(0, now_precision);
for (int i = precision - now_precision; i > 0; i--)
res = res + "0";
res = res + "*10^" + power;
}
return res;
}
void digit_equal()
{
int precision;
string a, b, res_a, res_b;
scanf("%d", &precision);
cin >> a >> b;
res_a = to_science_notation(a, precision);
res_b = to_science_notation(b, precision);
//cout << res_a << endl << res_b;
if (res_a == res_b) {
cout << "YES " << res_a;
}
else {
cout << "NO " << res_a << " " << res_b;
}
}
void use_map()
{
map<string, int> m;
m["jj"] = 1;
m["hh"] = 2;
m["ha"] = 3;
m["a"] = 100;
map<string, int>::iterator it;
for (it = m.begin(); it != m.end(); it++)
cout<<it->first<<":"<<it->second<<"\t";
cout<<endl<<m["hh"]<<endl;
it = m.find("hh");
m.erase(it);
m.erase("a");
for (it = m.begin(); it != m.end(); it++)
cout << it->first << ":" << it->second << "\t";
}
void use_queue()
{
queue<string> q;
q.push("hah");
q.push("jjjk");
q.push("zzmm");
cout <<"front:" << q.front()<<endl;
cout << "back:" << q.back() << endl;
while (!q.empty()) {
cout << q.front() << "\t";
q.pop();
}
}
struct fruit {
string name;
int price;
friend bool operator < (fruit f1, fruit f2) {
return f1.price < f2.price;
}
};
void use_priority_queue()
{
priority_queue<string> pq;
pq.push("jka");
pq.push("kkkh");
pq.push("jkjk");
cout << "top:"<<pq.top()<<endl;
while (!pq.empty()) {
cout << pq.top() << "\t";
pq.pop();
}
cout << endl;
priority_queue<string, vector<string>, greater<string> > pqg;//greater需要include<fuctional>
pqg.push("jka");
pqg.push("kkkh");
pqg.push("jkjk");
cout << "top:" << pqg.top() << endl;
priority_queue<fruit> fpq;
fruit f1, f2, f3;
f1.name = "apple";
f1.price = 5;
f2.name = "banana";
f2.price = 2;
f3.name = "pear";
fpq.push(f1);
fpq.push(f2);
fpq.push(f3);
while (!fpq.empty()) {
cout << fpq.top().name<<":"<<fpq.top().price << "\t";
fpq.pop();
}
cout << endl;
}
void use_stack()
{
stack<string> s;
s.push("ankl");
s.push("bjkld");
s.push("ckk");
while (!s.empty()) {
cout << s.top() << "\t";
s.pop();
}
}
void use_pair()
{
pair<int, int> p1(3, 5);
pair<int, int> p2(3, 10);
pair<int, int> p3(5, 7);
pair<int, int> p_a[3] = { p1, p2, p3 };
sort(p_a, p_a + 3);
for (int i = 0; i < 3; i++)
printf("%d,%d\t", p_a[i].first, p_a[i].second);
printf("\n");
map<string, int> m;
m.insert(make_pair("jjj", 1));
m.insert(make_pair("gdj", 1));
m.insert(pair<string, int>("hj",3));
map<string, int>::iterator it;
for (it = m.begin(); it != m.end(); it++) {
cout << it->first << ":" << it->second << "\t";
}
}
void algorithm_fun()
{
int a = 34, b=56;
swap(a, b);
printf("%d %d\n", a, b);
int num[10] = { 1,2,3,4,5,6 ,0,0,1};
reverse(num+1, num + 3);
fill(num + 6, num + 8, 666);
for (int i = 0; i < 10; i++)
printf("%d ", num[i]);
printf("\n");
printf("lower_bound27-4:%d, upper_bound27-4:%d\n", lower_bound(num + 2, num + 7, 4)-num,
upper_bound(num + 2, num + 7, 4)-num);//大于等于和大于
string s = "abcdef";
reverse(s.begin() + 3, s.end());
cout << s << endl;
do {
printf("%d %d %d\n", num[0], num[1], num[2]);
} while (next_permutation(num, num + 3));
vector<int> v;
v.push_back(89);
v.push_back(39);
v.push_back(290);
sort(v.begin(), v.end());
vector<int>::iterator it=v.begin();
for (int i = 0; i < v.size(); i++)
printf("%d ", *(it + i));
printf("\n");
}
int main()
{
//use_vector();
//use_set();
//use_string();
//digit_equal();
//use_map();
//use_queue();
//use_priority_queue();
//use_stack();
//use_pair();
algorithm_fun();
return 0;
}