//有关 STL 标准模板库的函数
/* string 的 */
/*
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
void stringinit()
{
string s1; //无参构造函数
string s2("helloworld"); //有参构造函数
string s3(10,'h'); //
string s4(s2); //拷贝构造函数
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
}
void stringat() //数据的存储
{
string s1("123456789");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
cout << s1[2] << endl; //可能会段错误(越界
cout << s1.at(1) << endl; //可以抛出异常(越界 具体如下
try
{
cout << s1.at(10) << endl;
}
catch (exception &e)
{
cout << e.what() << endl;
}
}
void stringlength()
{
string s1("helloworld");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
FOREGROUND_BLUE);
cout << s1.length() << endl;
string s2;
if (s2.empty())
{
cout << "empty string" << endl;
}
}
void stringstr()
{
string s1("helloworld");
const char *ptr = s1.c_str();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
FOREGROUND_BLUE);
cout << ptr << endl;
}
void stringcopy()
{
string s1("yohoo");
char buf[32] = {0};
s1.copy(buf,3,2);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
FOREGROUND_BLUE);
cout << buf << endl;
}
int main()
{
//stringinit();
//stringat();
//stringlength();
//stringstr();
//stringcopy();
system("pause");
return 0;
}
*/
/* vector */
/*
#include <iostream>
#include <vector>
#include <windows.h>
using namespace std;
void vectorinit()
{
int array[10] = {0,1,2,3,4,5,6,7,8,9};
vector<int> v1;
vector<int> v2(10);
vector<int> v3(array , array + sizeof(array) / sizeof(array[0]));
for (int i = 0; i < (int)v3.size(); i++)
{
cout << v3[i];
}
cout << endl;
vector<int> v4(v3);
for (int i = 0; i < (int)v4.size(); i++)
{
cout << v4[i];
}
cout << endl;
cout << v4.at(2) << endl;
cout << v4[5] << endl;
}
void vectorassgin()
{
int array[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
vector<int> v3(array, array + sizeof(array) / sizeof(array[0]));
vector<int> v1;
v1.assign(array, array + sizeof(array) / sizeof(array[0]));
for (int i = 0; i < (int)v1.size(); i++)
{
cout << v1[i];
}
cout << endl;
v1.assign(5 , 'a');
for (int i = 0; i < (int)v1.size(); i++)
{
cout << v1[i];
}
cout << endl;
v1.swap(v3);
for (int i = 0; i < (int)v1.size(); i++)
{
cout << v1[i];
}
cout << endl;
}
void vectoriterator()
{
vector<int> v1(10,0);
for (int i = 0; i < (int)v1.size(); i++)
{
v1[i] = i + 1;
}
for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
{
cout << *it;
}
cout << endl;
}
void vectorinsert()
{
}
int main()
{
//vectorinit();
//vectorassgin();
vectoriterator();
vectorinsert();
system("pause");
return 0;
}
*/
/* deque */
/*
#include <iostream>
#include <deque>
#include <windows.h>
using namespace std;
int main()
{
/*
deque <int> d1;
d1.push_back(1);
d1.push_back(2);
d1.push_back(3);
d1.push_back(4);
d1.push_back(5);
d1.push_front(99);
for (deque<int>::iterator it = d1.begin(); it != d1.end(); it++)
{
cout << *it<<" ";
}
cout << endl;
*/
/*
int array[10] = {3,1,4,7,3,6,4,3,1,3};
deque <int> d1(array,array + sizeof(array) / sizeof(array[0]));
for (deque<int>::iterator it = d1.begin(); it != d1.end();)
{
if (*it == 3)
{
it = d1.erase(it);
}
else
{
it++;
}
}
for (deque<int>::iterator it = d1.begin(); it != d1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
system("pause");
return 0;
}
*/
/* stack */
#include <iostream>
#include <stack>
using namespace std;
int Priority(char ch)
{
switch(ch)
{
case '(':
return 3;
case '*':
case '/':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
}
}
int main()
{
int i = 0, tmp = 0, num1, num2, result;
stack<int> s_opt, s_num;
char opt[64] = {0};
char ch;
cout << "Please input :" << endl;
cin >> opt;
while (opt[i] != '\0' || s_opt.empty() != true)
{
if (opt[i] >= '0' && opt[i] <= '9')
{
tmp = tmp * 10 + opt[i] - '0';
i++;
if (opt[i] > '9' || opt[i] < '0')
{
s_num.push(tmp);
tmp = 0;
}
}
else
{
//操作符进栈
if (s_opt.empty() || (s_opt.top() == '(' && opt[i] != ')')
|| Priority(opt[i]) > Priority(s_opt.top()))
{
s_opt.push(opt[i]);
i++;
continue;
}
if (s_opt.top() == '(' && opt[i] == ')')
{
s_opt.pop();
i++;
continue;
}
if ((opt[i] == ')' && s_opt.top() != '(') ||
Priority(opt[i]) <= Priority(s_opt.top()) || (opt[i] == '\0' && !s_opt.empty()))
{
ch = s_opt.top();
s_opt.pop();
switch(ch)
{
case '+':
num1 = s_num.top();
s_num.pop();
num2 = s_num.top();
s_num.pop();
s_num.push(num1 + num2);
break;
case '-':
num1 = s_num.top();
s_num.pop();
num2 = s_num.top();
s_num.pop();
s_num.push(num2 - num1);
break;
case '*':
num1 = s_num.top();
s_num.pop();
num2 = s_num.top();
s_num.pop();
s_num.push(num1 * num2);
break;
case '/':
num1 = s_num.top();
s_num.pop();
num2 = s_num.top();
s_num.pop();
s_num.push(num2 / num1);
break;
}
}
}
}
result = s_num.top();
cout << result << endl;
return 0;
}