C++常用STL库的简单学习

string

#include<string>
#include<iostream>

using namespace std;

int main()
{
	

	string s1;//初始化字符串,空字符串

	cin >> s1;

	string s2 = s1; //拷贝初始化,深拷贝字符串
	string s3 = "I am Yasuo"; //直接初始化,s3存了字符串
	string s4(10, 'a'); //s4存的字符串是aaaaaaaaaa
	string s5(s4); //拷贝初始化,深拷贝字符串
	string s6("I am Ali"); //直接初始化
	string s7 = string(6, 'c'); //拷贝初始化,cccccc

								//string的各种操作
	string s8 = s3 + s6;//将两个字符串合并成一个
	s3 = s6;//用一个字符串来替代另一个字符串的对用元素

	



	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	cout << s5 << endl;
	cout << s6 << endl;
	cout << s7 << endl;
	cout << s8 << endl;
	cout << "s7 size = " << s7.size() << endl; //字符串长度,不包括结束符
	cout << (s2.empty() ? "This string is empty" : "This string is not empty") << endl;

	string str("Hi sysu!");

	for (string::iterator it = str.begin(); it != str.end(); it++) {
		cout << *it << endl;
	}

	//寻找字串
	string sq("heoolo sdaa ss");
	cout << sq.find("eo", 0) << endl;
	//若未找到则返回   string::npos
	if (sq.find("aa1", 0) == string::npos) {
		cout << "未找到" << endl;
	}
	


	return 0;

}

在这里插入图片描述

vector

#include <iostream>
#include <vector>
#include <string>

using namespace std;

template <typename T>
void showvector(vector<T> v)
{
	for (vector<T>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it;
	}
	cout << endl;
}

int main()
{
	vector<string> v6 = { "hi","my","name","is","lee" };
	v6.resize(3);  //重新调整vector容量大小
	showvector(v6);

	vector<int> v5 = { 1,2,3,4,5 }; //列表初始化,注意使用的是花括号
	cout << v5.front() << endl; //访问第一个元素
	cout << v5.back() << endl; //访问最后一个元素

	showvector(v5);
	v5.pop_back(); //删除最后一个元素
	showvector(v5);
	v5.push_back(6); //加入一个元素并把它放在最后
	showvector(v5);
	v5.insert(v5.begin() + 1, 9); //在第二个位置插入新元素
	showvector(v5);
	v5.erase(v5.begin() + 3);  //删除第四个元素
	showvector(v5);
	v5.insert(v5.begin() + 1, 7, 8); //连续插入7个8
	showvector(v5);
	v5.clear(); //清除所有内容
	showvector(v5);

	return 0;
}

C++中push_back和insert两个有什么区别?

顾名思义push_back把元素插入容器末尾,insert把元素插入任何你指定的位置。
不过push_back速度一般比insert快。如果能用push_back尽量先用push_back。

set

#include <iostream>
#include <set>
#include <string>

using namespace std;

template <typename T>
void showset(set<T> v)
{
    for (set<T>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it;
    }
    cout << endl;
}

int main()
{
    set<int> s1{9,8,1,2,3,4,5,5,5,6,7,7 }; //自动排序,从小到大,剔除相同项
    showset(s1);
    set<string> s2{ "hello","sysy","school","hello" }; //字典序排序
    showset(s2);
    s1.insert(9); //有这个值了,do nothing
    showset(s1);
    s2.insert("aaa"); //没有这个字符串,添加并且排序
    showset(s2);
    
    system("pause");
    return 0;
} 

set跟vector差不多,它跟vector的唯一区别就是,set里面的元素是有序的且唯一的,只要你往set里添加元素,它就会自动排序,而且,如果你添加的元素set里面本来就存在,那么这次添加操作就不执行。要想用set先加个头文件set。

list

#include <iostream>
#include <list>
#include <string>

using namespace std;

template <typename T>
void showlist(list<T> v)
{
    for (list<T>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << *it;
    }
    cout << endl;
}

int main()
{
    list<int> l1{ 1,2,3,4,5,5,6,7,7 };
    showlist(l1);
    list<double> l2;
    list<char> l3(10);
    list<int> l4(5, 10); //将元素都初始化为10
    showlist(l4);

    
    system("pause");
    return 0;
} 

list即双向链表的优点是插入和删除元素都比较快捷,缺点是不能随机访问元素。

初始化方式就大同小异了,跟vector基本一样。要想用list先加个头文件list。

map

map运用了哈希表地址映射的思想,也就是key-value的思想,来实现的。

首先给出map最好用也最最常用的用法例子,就是用字符串作为key去查询操作对应的value。

#include <iostream>
#include <map>
#include <string>

using namespace std;

void showmap(map<string, int> v)
{
    for (map<string, int>::iterator it = v.begin(); it != v.end(); it++)
    {
        cout << it->first << "  " << it->second << endl;  //注意用法,不是用*it来访问了。first表示的是key,second存的是value
    }
    cout << endl;
}

int main()
{
    map<string, int> m1; //<>里的第一个参数表示key的类型,第二个参数表示value的类型
    m1["Kobe"] = 100;
    m1["James"] = 99;
    m1["Curry"] = 98;

    string s("Jordan");
    m1[s] = 90;

    cout << m1["Kobe"] << endl;
    cout << m1["Jordan"] << endl;
    cout << m1["Durant"] << endl; //不存在这个key,就显示0

    m1.erase("Curry");//通过关键字来删除
    showmap(m1);
    m1.insert(pair<string, int>("Harris", 89)); //也可以通过insert函数来实现增加元素
    showmap(m1);
    m1.clear(); //清空全部


    system("pause");
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值