#include<iostream> #include<algorithm> #include<stack> #include<sstream> #include<string> using namespace std; //------十进制转化为二进制----- int itob(int decima) { stack<int> v; int res = 0; while (decima != 0) { v.push(decima % 2);//放入栈顶 decima /= 2;//依次除二 } while (!v.empty()) { res = res * 10 + v.top(); v.pop();//移除栈顶 }return res; } int main() { stack<int> s;//构造 s.push(1); s.push(2); s.push(3);//入栈一个元素 cout << s.top() << endl;//取栈顶元素 s.pop();//出栈一个元素 cout << s.top() << endl; s.push(5); cout << s.size() << endl;//查看元素个数 cout << itob(20) << endl;//输出20的二进制 //需要输入,影响后面的输出,先注释掉 /*//拓展sstream string str; stack<string> t; getline(cin, str);//iostream中的getline stringstream ss; ss << str;//流入 while (ss >> str) {//流出 t.push(str);//依次放入栈顶 } cout << t.top(); t.pop(); while (!t.empty()) { cout << ' ' << t.top(); t.pop(); }cout << endl;*/ //字符串转整型变量 string a = "1234"; int i; stringstream st;//使用stringstream转换类型 st << a; st >> i; cout << i << endl; //int j = stoi(s); //cout << i << endl; //整型变量转字符串 //方法1同上用stringstream //方法2: int b = 2345; cout << to_string(b) << endl;//使用to_string进行转换 return 0; }