Vector:

 #include "stdafx.h"
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v = { ,,,,,, };
cout << "排序前的数组: ";
for (int elem : v) {
cout << elem << " "; //如何输出
}
cout << endl;
stable_sort(v.begin(), v.end()); //此排序方式为稳定排序,适用于v中有相同元素,如上面有2个66的情况,
//这样就不会改变两个66的相对位置。如果没有重复元素的话,用sort(v.begin(), v.end())方可
cout << "排序后的数组: ";
for (int elem : v) {
cout << elem << " "; //如何输出
}
cout << endl;
fill(v.begin()+, v.end(), ); //填充
cout << "此时的数组: ";
for (int elem : v) {
cout << elem << " "; //如何输出
}
cout << endl;
fill_n(v.begin(), , ); //填充的另一种方式
cout << "此时的数组: ";
for (int elem : v) {
cout << elem << " "; //如何输出
}
cout << endl;
return ;
}

对于上述输出方式的演变如下:现在用(3)即可

(1)

 for_each (int elem in v)
{
cout << elem << " ";
}

(2)

 for_each (v.begin(),v.end(),[](int elem)
{
cout << elem << " ";
}

(3)

 for (int elem : v)
{
cout << elem << " ";
}

字符串大写变小写:

 #include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string s1 = "hello world.";
string s2;
transform(s1.begin(), s1.end(), s2.begin(), toupper); //同样大写变小写就是tolower
cout << s1 << endl;
}

二分查找:

 #include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v= { ,,,,,, };
if (binary_search(v.begin(), v.end(),))
cout << "Exits!" << endl;
else
cout << "Didn't exits!" << endl;
}

数列反转:

 #include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v= { ,,,,,, };
cout << "反转前的数列为: ";
for (int elem : v)
{
cout << elem << " ";
}
cout << endl;
reverse(v.begin(), v.end());
cout << "反转后的数列为: ";
for (int elem : v)
{
cout << elem << " ";
}
cout << endl;
return ;
}

条件分区:

 #include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v = { ,,,,,,,, };
stable_partition(v.begin(), v.end(), [](int elem)
{
return elem % == ;
});
for (int elem : v)
{
cout << elem << " ";
}
cout << endl;
return ;
}

运行结果:

c++ STL(2)的更多相关文章

  1. 详细解说 STL 排序(Sort)

    0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...

  2. STL标准模板库(简介)

    标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...

  3. STL的std::find和std::find_if

    std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. 小例子: #include "stdafx.h" #i ...

  4. STL: unordered_map 自定义键值使用

    使用Windows下 RECT 类型做unordered_map 键值 1. Hash 函数 计算自定义类型的hash值. struct hash_RECT { size_t operator()(c ...

  5. C++ STL简述

    前言 最近要找工作,免不得要有一番笔试,今年好像突然就都流行在线笔试了,真是搞的我一塌糊涂.有的公司呢,不支持Python,Java我也不会,C有些数据结构又有些复杂,所以是时候把STL再看一遍了-不 ...

  6. codevs 1285 二叉查找树STL基本用法

    C++STL库的set就是一个二叉查找树,并且支持结构体. 在写结构体式的二叉查找树时,需要在结构体里面定义操作符 < ,因为需要比较. set经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...

  7. STL bind1st bind2nd详解

    STL bind1st bind2nd详解   先不要被吓到,其实这两个配接器很简单.首先,他们都在头文件<functional>中定义.其次,bind就是绑定的意思,而1st就代表fir ...

  8. STL sort 函数实现详解

    作者:fengcc 原创作品 转载请注明出处 前几天阿里电话一面,被问到STL中sort函数的实现.以前没有仔细探究过,听人说是快速排序,于是回答说用快速排序实现的,但听电话另一端面试官的声音,感觉不 ...

  9. STL的使用

    Vector:不定长数组 Vector是C++里的不定长数组,相比传统数组vector主要更灵活,便于节省空间,邻接表的实现等.而且它在STL中时间效率也很高效:几乎与数组不相上下. #include ...

  10. [C/C++] C/C++延伸学习系列之STL及Boost库概述

    想要彻底搞懂C++是很难的,或许是不太现实的.但是不积硅步,无以至千里,所以抽时间来坚持学习一点,总结一点,多多锻炼几次,相信总有一天我们会变得"了解"C++. 1. C++标准库 ...

随机推荐

  1. Python | 用Pyinstaller打包发布exe应用

    参考:https://jingyan.baidu.com/article/a378c960b47034b3282830bb.html https://ask.csdn.net/questions/72 ...

  2. python2.7练习小例子(十五)

        15):题目:输出指定格式的日期.     程序分析:使用 datetime 模块.     程序源代码: #!/usr/bin/python # -*- coding: UTF-8 -*- ...

  3. Word中使用宏调整表格

    Dim i As Integer   For i = 1 To Selection.Tables.Count Selection.Tables(i).Columns(9).Delete Selecti ...

  4. mysql 导入CSV数据 [转]

    转自: http://blog.chinaunix.net/uid-23284114-id-3196638.html MYSQL   LOAD DATA INFILE命令可以把csv平面文件中的数据导 ...

  5. 在Linux下通过rpm打包发布Java程序

    这个东西涉及的内容较多,根据下面这些文章慢慢学习 一个简单的例子 http://blog.csdn.net/king_on/article/details/7169384 按照文章中的步骤来,打包之后 ...

  6. abo dto属性验证的坑

    问题回现: public class ShipmentRequestDto { public string FromPhoneNumber { get; set; } /// <summary& ...

  7. Qt HUD(平显)演示程序绿色版

    把一个黑底白字的程序改成黑底绿字 上对比图,左侧是原本,右侧是仿造,有些地方比例还是有问题 其实这个程序没有啥技术含量,就是画 #include "mainwindow.h" #i ...

  8. JAVA虚拟机(一):内存区域

    根据<java虚拟机规范第二版>规定,现阶段的java内存区域总体如下图 其中,方法区和堆是所有线程共享区域. 虚拟机栈,本地方法栈,程序计数器是各线程独占. 概述一下各个区域 先说说线程 ...

  9. 测试理论-selenium的工作原理

  10. HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)

    Problem Description In this problem, you are given several strings that contain only digits from '0' ...