補8-5日複習內容 STL 標準模板庫的容器

//有關 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;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章