栈的输入输出
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<int> a;
int n;
cin >> n;
for(int i=0;i<n;i++)
{
int m;
cin>>m;
a.push(m);
}
cout << a.top() << endl;
while( !a.empty() )
{
cout << a.top() << " ";
a.pop();
}
return 0;
}
队列的输入和输出
#include<iostream>
#include<queue>
using namespace std;
int main()
{
queue<int> a;
int n;
cin >> n;
for(int i=0;i<n;i++)
{
int m;
cin >> m;
a.push(m);
}
while(!a.empty())
{
cout << a.front() << endl;
a.pop();
}
return 0;
}
数据库的插入和出处
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> s;
int n;
cin >> n;
for(int i=0;i<n;i++)
{
int m;
cin >> m;
s.push_back(m);
}
for(int i=0;i<s.size();i++)
{
cout << s.at(i) <<" ";
} cout <<endl;
s.insert(s.begin(),122);//插入一个数(122)在第一个元素那;
s.erase(s.begin()+2);//删除第三个元素;(2+1)
for(int i=0;i<s.size();i++)
{
cout << s.at(i) <<" ";
}
cout << endl;
while( !s.empty() )//倒
{
cout << s.back() << " ";
s.pop_back();
}
}
字符串的输入与输出
//字符串的数据库运用
//等把这些搞会后就做题
#include<iostream>
#include<string>
using namespace std;
int main()
{/*字符串的构造函数创建一个新字符串,包括:
以length为长度的ch的拷贝(即length个ch)
以str为初值 (长度任意),
以index为索引开始的子串,长度为length, 或者
以从start到end的元素为初值.
*/
string str1( 5, 'c' );
string str2( "Now is the time..." );
string str3( str2, 11, 4 );
cout << str1 << endl;//ccccc
cout << str2 << endl;//Now is the time...
cout << str3 << endl;//time
string str4 = "Hello World";//添加文本
str4.append( 10, '!' );
cout << str4 << endl;
string str11, str21 = "War and Peace";
str1.assign( str21, 4, 3 ); //赋值,第四个起连续三个 子串赋值给str2;
cout << str11 << endl;//and;
string text = "ABCDEF";
char ch = text.at( 2 );//取值
//字符串的查找
string str13( "Alpha Beta Gamma Delta" );
unsigned int loc = str13.find( "Omega", 0 );
if( loc != string::npos )
cout << "Found Omega at " << loc << endl;
else
cout << "Didn't find Omega" << endl;
//替换
string s7 = "They say he carved it himself...from a BIGGER spoon";
string s27 = "find your soul-mate, Homer.";
s7.replace( 32, s27.length(), s27 );
cout << s7 << endl;
//截取字符串
//substr()返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串。
string s5("What we have here is a failure to communicate");
string sub = s5.substr(21);
cout << "The original string is " << s5 << endl;
cout << "The substring is " << sub << endl;
/*显示:
The original string is What we have here is a failure to communicate
The substring is a failure to communicate
*/
//交换
string first( "This comes first" );
string second( "And this is second" );
first.swap( second );
cout << first << endl;
cout << second << endl;
}
/* 大概就这么多 */
用栈进行10进制转换二进制
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<int> s;
int m;
cin>>m;
while(m)
{
int n;
n=m%2;
s.push(n);
m=m/2;
}
while(!s.empty())
{
cout<< s.top();
s.pop();
}
return 0;
}
数据库方式进行二进制转换
#include<bits/stdc++.h>
using namespace std;
int main()
{
int num;
cout << “请输入要转换2进制的数: “;
cin >> num;
char str[100];
_itoa(num, str, 2); //c++中一般用_itoa,用itoa也行,
cout <<“转换结果为: “<<str<<endl;
return 0;
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/114914.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...