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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • word文档怎么打印成册子_零基础学word文档

    word文档怎么打印成册子_零基础学word文档Word2003是微软公司提供的一款文字处理软件,可以对文字进行排版和编辑、分段等各种处理,最终将编辑好的内容打印出来,是办公室一族中必备的办公软件之一。Word2003具有一个“书籍折页”的功能,能够在一页纸上打印两页的内容,打印后可以从中缝装订成折叠的小册子,更便于携带和阅读,具体怎么来设置呢?就让Word联盟为大家带来分享!动画演示:①打开“页面设置”对话框,切换到

    2025年9月18日
    6
  • 如何求逆矩阵_副对角线矩阵的逆矩阵怎么求

    如何求逆矩阵_副对角线矩阵的逆矩阵怎么求作为一只数学基础一般般的程序猿,有时候连怎么求逆矩阵都不记得,之前在wikiHow上看了一篇不错的讲解如何求3×3矩阵的逆矩阵的文章,特转载过来供大家查询以及自己备忘。当然这个功能在matlab里面非常容易实现,只要使用inv函数或A^-1即可,但是有时候参加个考试什么的还是要笔算的哈哈~假设有如下的3×3矩阵,第一步需要求出det(M),也就是矩阵M的行列式的值。行列式的值通常显示

    2022年8月21日
    13
  • springboot集成了哪些框架_redis java客户端

    springboot集成了哪些框架_redis java客户端Springboot集成Redis添加Redis依赖<depency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!–连接池–>

    2022年9月22日
    2
  • ip addr命令详解_c++中add是什么意思

    ip addr命令详解_c++中add是什么意思OverviewTheipcommandisaLinuxnet-toolforsystemandnetworkadministators.TheipcommandreplacesoldgoodandnowdeprecatedifconfigcommandonmodernLinuxdistributions.用法ip[options]OBJECTCOMMANDipaddr#查看ipethomeansEthernet,wl..

    2022年7月27日
    6
  • WiFi的2.4G、5G、6G频段「建议收藏」

    WiFi的2.4G、5G、6G频段「建议收藏」目前WiFi已经推出了6G频段,Android源码中也增加了相关的功能,这里总结一下。2.4G一共分为14个信道(1-14),从2412到2484,每个信道的有效宽度是20MHz,另外还有2MHz的强制隔离频带(类似于公路上的隔离带)。即,对于中心频率为2412MHz的1信道,其频率范围为2401~2423MHz。5G一共有60个信道(32-173),从5160到5865,在中国支持的5G信道为363840444648525456606264,后六个是DFS。6G为1-2

    2022年10月20日
    3
  • beanUtils.copy_拷贝板原理

    beanUtils.copy_拷贝板原理本文用实例介绍BeanUtils的拷贝原理。

    2025年8月28日
    4

发表回复

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

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