stl库,基本操作代码

栈的输入输出

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值