stl

C++模板库STL——vector

别来无恙 提交于 2020-01-20 19:12:03
终于开始c++了,激动! 总结: vector作为可变数组使用,可以方便统计数组中元素个数; 插入、删除数据都很方便; 注意删除区间的时候是左闭右开; # include <stdio.h> # include <vector> using namespace std ; int main ( ) { vector < int > vi ; for ( int i = 6 ; i <= 15 ; i ++ ) { vi . push_back ( i ) ; } for ( int i = 0 ; i < vi . size ( ) ; i ++ ) { printf ( "%d " , vi [ i ] ) ; } printf ( "\n" ) ; //vi从0开始计数的vi+2的位置 vi . insert ( vi . begin ( ) + 2 , - 1 ) ; for ( int i = 0 ; i < vi . size ( ) ; i ++ ) { printf ( "%d " , vi [ i ] ) ; } printf ( "\n" ) ; //删除一个位置的数 vi . erase ( vi . begin ( ) + 3 ) ; for ( int i = 0 ; i < vi . size ( ) ; i ++ ) { printf ( "%d " ,

converting an array of null terminated const char* strings to a std::vector< std::string >

落爺英雄遲暮 提交于 2020-01-20 05:43:08
问题 I have a Visual Studio 2008 C++ function where I'm given an array of null-terminated strings const char* and a count of the number of strings in that array. I'm looking for a clever way of turning an array of const char* in to a std::vector< std::string > /// @param count - number of strings in the array /// @param array - array of null-terminated strings /// @return - a vector of stl strings std::vector< std::string > Convert( int count, const char* array[] ); Boost is fine, STL is fine.

STL之string的详细介绍

坚强是说给别人听的谎言 提交于 2020-01-20 01:24:35
什么是STL? STL(standard template libaray )标准模板库 是C++标准库的重要组成部分 不仅是一个可以复用的组件库 更是一个包罗数据结构算法的软件框架 以下是STL的六大组件 大概了解一下以后会详细介绍。 STL六大组件: 1.仿函数 如:greater less 2.算法 如:find swap reverse sort merge 3.迭代器 如:iterator const_iterator reverse_iterator const_reverse_iterator 4.空间配置器 如:allocator 5.容器 如:string vector list deque map set multimap mutilset 6.配接器 如:stack queue priority_queue 什么是string? string是表示 字符串的字符串类 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。 string在底层实际是: basic_string 模板类的别名 ,typedef basic_string<char, char_traits, allocator> string; 不能操作多字节或者变长字符的序列。 在使用string类时必须包含头文件 <string> 以及 using

STL好坑

我的梦境 提交于 2020-01-20 00:18:36
关于multiset,有如下结论: 1 multiset<int> s; 2 s.insert(3); 3 s.insert(3); 4 cout<<s.count(3);//结果输出2 5 s.erase(3); 6 cout<<s.count(3);//结果输出0 因此multiset模拟时应注意有几个。数据量小可以用哈希数组。 关于map,有如下结论: 1 map<int,int> mp; 2 int main(){ 3 mp[-1]=1; 4 mp.clear(); 5 cout<<mp[-1];//有的环境下,输出1 6 } 这说明map的clear是个坑,可以考虑用map.erase(map.begin(),map.end())。 来源: https://www.cnblogs.com/St-Lovaer/p/12216057.html

第二期STL题解

北城以北 提交于 2020-01-19 19:51:12
很水的题。 1. # include <iostream> # include <cstdio> using namespace std ; int main ( ) { string s , r ; cin >> s >> r ; if ( s > r ) cout << ">" << endl ; else if ( s == r ) cout << "=" << endl ; else cout << "<" << endl ; return 0 ; } # include <iostream> # include <algorithm> using namespace std ; int main ( ) { int n ; cin >> n ; getchar ( ) ; while ( n -- ) { string s , t = "" ; getline ( cin , s ) ; for ( int i = ( int ) s . size ( ) - 1 ; i >= 0 ; i -- ) { if ( i == 0 ) { t + = s [ i ] ; reverse ( t . begin ( ) , t . end ( ) ) ; cout << t << endl ; t = "" ; } else if ( s [ i ] == ' ' ) {

How to get a certain element in a list, given the position?

一曲冷凌霜 提交于 2020-01-19 04:42:17
问题 So I've got a list: list<Object> myList; myList.push_back(Object myObject); I'm not sure but I'm confident that this would be the "0th" element in the array. Is there any function I can use that will return "myObject"? Object copy = myList.find_element(0); ? 回答1: If you frequently need to access the Nth element of a sequence, std::list , which is implemented as a doubly linked list, is probably not the right choice. std::vector or std::deque would likely be better. That said, you can get an

To STL or !STL, that is the question [closed]

℡╲_俬逩灬. 提交于 2020-01-19 04:33:05
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 8 months ago . Unquestionably, I would choose to use the STL for most C++ programming projects. The question was presented to me recently however, "Are there any cases where you wouldn't use the STL?"... The more I thought about it, the more I realized that perhaps there SHOULD be cases

STL之sort应用

帅比萌擦擦* 提交于 2020-01-19 01:51:46
前言 顾名思义,sort()是标准库中的排序算法,能用此算法的容器是支持随机访问的容器:vector, deque, string,array。 其有两种形式,第一种sort(a,b),其中a,b为排序的地址范围,为[a,b),此时用operator<作比较,默认升序排序。 第二种sort(a,b,comp),此时采用comp进行比较,comp带两个同类型的参数,如果第一个参数排在第二个参数前面,返回true,否则返回false,它可以是函数指针,也可以是函数对象,即仿函数。 下为几个常见的仿函数: less(小于) greater(大于) equal_to(等于) not_equal_to(不相等) less_equal(小于等于) greater_equal(大于等于) 须注意不能直接写入仿函数的名字,而是要写其重载的()函数: less<>() greater<>() 常见sort分类 1.全排序 sort 和 stable_sort,前者不稳定,采用快排,后者为稳定排序,归并排序 2.部分排序 partial_sort,如part_sort(a,a+n,comp);只排前n个,采用堆排序,显然适用于求解topn问题。 结构体排序 针对两种sort的两种形式主要有两种应用形式。 1.运算符重载 采用sort(a,b),或者只想利用上述常见的伪函数,需要用到运算符重载。

STL——おみやげをまらいました!

旧巷老猫 提交于 2020-01-19 00:18:02
STL——おみやげをまらいました! 题目描述 蛙蛙还是给你带来了礼物。但它有个小小的要求,那就是你得在石头剪刀布上赢过它才能拿到礼物哦! 你们这样规定:有三个字符串 S1,S2,S3,表示三种出拳种类,其中S1 ​可以打败 S2,S2可以打败 S3,S3 可以打败 S1 。 现在根据你的观察,你已经知道了蛙蛙心想的出拳顺序,你需要安排自己的出拳顺序,使得你能在每一局中都获胜。 输入描述 前三行每行两个字符串,每行Sa,Sb,表示Sa能打败Sb。 数据保证不互相矛盾,且其中有恰好三种不同的字符串。 接下去一个数N,表示N次对战。接下去N行每行一个字符串,表示蛙蛙的出拳种类。注意,蛙蛙的出拳可能不合法(即不是三种字符串中的),这时请输出“Fake”。 输出描述 共N行,每行一个字符串,表示每一局你需要出什么。 如果对手出的不合法,输出“Fake”。 示例 输入 stone sci sci paper paper stone 4 stone sci spock paper 输出 paper stone Fake sci 备注 2<=|S|<=50 1<=N<=100 分析 这题用map求解就行。 方法一: # include <cstdio> # include <algorithm> # include <map> # include <string> # include

泛型程序设计,STL标准模板库

非 Y 不嫁゛ 提交于 2020-01-18 04:41:06
泛型程序:将程序从特定的数据结构中抽象出来,可扩展性非常好 迭代器,适配器 1.泛型程序设计 概念:具有一定功能的数据类型 模型:符合一个概念的数据类型 2.STL标准模板库(Standard Template Library) 3.迭代器:迭代容器中的元素 4.容器 逆向迭代器: rbegin():将容器的尾作为头逆向来用,指向的是容器的尾,遍历时,可以从尾到头的方向来遍历。 rend():将容器的头作为尾,指向容器的首。 6.顺序容器的基本功能 将列表中相邻两个元素的位置颠倒: 先将该元素存放在一个临时变量里面, 将该元素删除,删除后指向该元素的指针指向其下一个位置 将临时变量里面的元素插入到指针所指向的位置的后面 插入完,指针指向下一个位置 7.顺序容器的特点 利用双端队列来实现奇数在前,偶数在后,并且奇数按照从大到小的顺序输出,偶数按照从小到大的顺序输出 将读入的数据进行排序 遍历存放输入元素的迭代器,将奇数前插在队列的前端,偶数采用后插放在队列的尾部 8.顺序容器的插入迭代器与适配器 适配器:在已有容器的基础之上构建一些特殊的容器,例如:栈、队列是在顺序容器的基础上进行构建的 特别注意:栈和队列不支持迭代器,因为他们不提供对任意一个元素进行随机访问的操作 9.关联容器 10.映射 11.多重集合与多重映射 12.函数对象 12.函数适配器 分类: mem