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


相关推荐

  • SpringBoot使用@Mapper和@MapperScan注解无效的解决方法

    SpringBoot使用@Mapper和@MapperScan注解无效的解决方法在使用@Mapper注解时,注解无效,service层,dao层该添加的注解都添加了,最后发现是少加了一个jar包&amp;lt;!–缺少此jar包,导致@Mapper注解无效–&amp;gt;&amp;lt;dependency&amp;gt;&amp;lt;groupId&amp;gt;org.mybatis.spring.boot&amp;lt;/groupId&amp;gt;

    2022年6月10日
    56
  • python实现 猴子摘香蕉「建议收藏」

    python实现 猴子摘香蕉「建议收藏」#猴子摘香蕉importsys#找到箱子defmove():globaliwhileTrue:a_1=input(“输入你下步走的地方:”)whileTrue:ifa_1==b:i+=1print(‘找到箱子,通过第一关,进入第二关’)push()else:.

    2022年9月26日
    0
  • Spring-data-JPA详细介绍,增删改查实现「建议收藏」

    Spring-data-JPA详细介绍,增删改查实现「建议收藏」本篇进行Spring-data-jpa的介绍,几乎涵盖该框架的所有方面,在日常的开发当中,基本上能满足所有需求。这里不讲解JPA和Spring-data-jpa单独使用,所有的内容都是在和Spring整合的环境中实现。如果需要了解该框架的入门,百度一下,很多入门的介绍。在这篇文章的接下来一篇,会有一个系列来讲解mybatis,这个系列从mybatis的入门开始,到基本使用,和spring整合,和第

    2022年6月7日
    36
  • sanitizer工具集

    sanitizer工具集sanitizer工具集的介绍Sanitizers是谷歌发起的开源工具集,包括了AddressSanitizer,undefinedbehaviorSanitizer,ThreadSanitizer,LeakSanitizer。GCC从4.8版本开始支持Addresssanitizer和ThreadSanitizer,4.9版本开始支持LeakSanitizer和undefinedbehaviorSanitizer。AddressSanitizer(ASAN):  也即地址

    2025年7月9日
    0
  • 验证市场可行性(PMF)的5个步骤[通俗易懂]

    验证市场可行性(PMF)的5个步骤[通俗易懂]在增长黑客的理念中,一切的“猜想”和“创意”都是需要经过验证的,用事实来证明猜想和创意是否可行,这其实也是增长黑客的特质之一,将所有不可量化的东西转化为可量化的评估标准。比如如何证明你的创意能够成功呢?验证PMF的其中一个标准是调研你的用户,如果40%的核心用户认为缺了你的产品会很遗憾,而不是可有可无,那么这就说明找到了P/MF;PMF到底是什么呢?你可以理解为一个指标,例如40%的用户认为没…

    2022年5月23日
    45
  • 无线充电原理与QI协议详解[通俗易懂]

    一、无线充电基本原理无线充电的基本原理就是我们平时常用的开关电源原理,区别在于没有磁介质耦合,那么我们需要利用磁共振的方式提高耦合效率,具体方法是在发送端和接收端线圈串并联电容,是发送线圈处理谐振状态,接收端线圈也是如此下图就是实际电路应用无线充电工作基本原理图发射板主要有控制ic,驱动ic,发射线圈,谐振电容组成这个是接收线圈,扎数比发射线圈多所以谐振电容可以小一些,方便安装…

    2022年4月4日
    2.6K

发表回复

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

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