stl库,基本操作代码

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/114914.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • plot函数详解_plot函数参数

    plot函数详解_plot函数参数plot(X,Y):绘制Y关于X的函数。plot(X,Y,LineSpec):指定线形,标记,颜色等。详细plot(_,Name,Value):指定线的属性。举例:plot(x,y1,’k-‘,x,y2,’b–‘,x,y3,’r:’,’linewidth’,1.5);figure(1)plot(x,Psum,’k-‘,x,Pz1,’b–‘,x,Pr1,’r:’,’line

    2022年10月15日
    0
  • linux修改密码后登陆失败_linux取消root密码

    linux修改密码后登陆失败_linux取消root密码问题:当使用root修改密码时,报错passwd:Authenticationtokenmanipulationerror解决:1、查看是否权限问题,/etc/passwd/etc/shadow文件是否被锁住lsattr/etc/passwdlsattr/etc/shadow文件解锁:chattr-i/etc/passwdchattr-i/etc…

    2022年9月5日
    3
  • HTML5视频直播技术介绍

    HTML5视频直播技术介绍视频直播如火如荼,为了满足比较火热的移动Web端直播需求,一系列的HTML5直播技术迅速的发展了起来。只要实现了视频直播的各个技术难点,通过HTML5进行视频直播并非难事。常见的可用于HTML5的直播技术共有3种协议:HLS、WebSocket与WebRTC。本文将对基于这3种协议的HTML5直播技术实现做下基础的介绍。一.HLS优点:CDN支持比较好缺点

    2022年7月21日
    15
  • flume和kafka区别

    flume和kafka区别kafka和flume都是日志系统,kafka是分布式消息中间件,自带存储;flume每一部分都是可以定制。kafka更合适做日志缓存,flume数据采集部分做的很好,可以定制很多数据源,减少开发量。kafka和flume都是日志系统,kafka是分布式消息中间件,自带存储,提供push和pull存取数据功能。flume分为agent(数据采集器),collector(数据简单处理和写入),storage(存储器)三部分,每一部分都是可以定制的。比如agent采用RPC(Thri.

    2022年6月23日
    24
  • 常见分布式文件存储介绍、选型比较、架构设计

    常见分布式文件存储介绍、选型比较、架构设计Hello,我是瓜哥:之前在进行对接存储项目的时候,对公司内部使用的文件系统进行了梳理,当前公司内部使用的文件系统有GlusterFS,FastDFS等,由于文件系统在海量小文件和高并发之下性能急剧下降,性能遭遇瓶颈,因此打算建设分布式对象存储平台。下面对市面上比较流行的非结构化文件存储产品进行相关整理和比较。分布式文件存储的来源在这个数据爆炸的时代,产生的数据量不断地在攀升,从GB,…

    2022年6月10日
    43
  • Java安全之Commons Collections3分析

    Java安全之CommonsCollections3分析文章首发:Java安全之CommonsCollections3分析0x00前言在学习完成前面的CC1链和CC2链后,其实再来看CC3

    2021年12月12日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号