函数调用操作(c++语法中的左右小括号)可以被重载,STL的特殊版本都以仿函数形式呈现。如果对某个class进行operator()重载,它就成为一个仿函数。

仿函数(functor),就是使一个类的使用看上去象一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了

#include <iostream>
using namespace std; template<class T>
struct Plus
{
T operator()(const T& x, const T& y)const
{
return x + y;
}
}; template<class T>
struct Minus
{
T operator()(const T& x, const T& y)const
{
return x - y;
}
}; int main()
{
Plus<int>plusobj;
Minus<int>minusobj; cout << plusobj(, ) << endl;
cout << minusobj(, ) << endl; //以下直接产生仿函数的临时对象,并调用
cout << Plus<int>()(, ) << endl;
cout << Minus<int>()(, ) << endl; system("pause");
return ;
}

产生临时对象的方法:在类型名称后直接加一对小括号,并可指定初值

vector<int>() = {1};

。stl常将此技巧应用于仿函数与算法的搭配上。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; /*
template <class InputIterator,class Function>
Function for_each(InputIterator first, InputIterator last, Function f)
{
for (; first != last; ++first)
{
f(*first);
}
return f;
}
*/ template <typename T>
class print
{
public:
void operator()(const T& elem)
{
cout << elem << ' ' << endl;
}
}; int main()
{
int ia[] = { ,,,,, };
vector<int>iv(ia, ia + ); for_each(iv.begin(), iv.end(), print<int>()); system("pause");
return ;
}

静态常量整数成员在class内部直接初始化,否则会出现链接错误。

In C++11, non-static data members, static constexpr data members, and static const data members of integral or enumeration type may be initialized in the class declaration. e.g.

struct X {
int i=5;
const float f=3.12f;
static const int j=42;
static constexpr float g=9.5f;
};

In this case, the i member of all instances of class X is initialized to 5 by the compiler-generated constructor, and the f member is initialized to 3.12. The static const data member j is initialized to 42, and the static constexpr data member g is initialized to 9.5.

Since float and double are not of integral or enumeration type, such members must either be constexpr, or non-static in order for the initializer in the class definition to be permitted.

Prior to C++11, only static const data members of integral or enumeration type could have initializers in the class definition.

const 和 constexpr 变量之间的主要区别在于:const 变量的初始化可以延迟到运行时,而 constexpr 变量必须在编译时进行初始化。所有 constexpr 变量均为常量,因此必须使用常量表达式初始化。

对于修饰Object来说,const并未区分出编译期常量和运行期常量,constexpr限定在了编译期常量。

编译器常量的特点就是:它的值在编译期就可以确定。比如:const int i = 5;

在运行时常量,它的值虽然在运行时初始化后不再发生变化,但问题就在于它的初始值要到编译时才能确定。比如:
srand(clock());
const int i = rand();
虽然i的值在定义并初始化成不会再发生变化(除非你使用一些不符合标准的小技巧),但再聪明的编译器也无法在编译时确定它的值。

编译期常量最常见的例子是编译时的常数定义,比如:
const double PI = 3.1415926;
运行期常量的最常见的例子是函数的常量参数(包括常引用,常指针参数)比如:
void f(const string& s) {...}
次常见的例子是类的非静态常量成员。
——这些都是一经初始化,不允许再发生变化的,但其初始值必须到运行时才能知道。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; template <typename T>
class testClass
{
public:
static const int a = ;
static const long b = 3L;
static const char c = 'c';
static const double d = 100.1;
}; int main()
{
cout << testClass<int>::a << endl;
cout << testClass<int>::b << endl;
cout << testClass<int>::c << endl;
//下面的语句出错,带有类内初始值设定项的静态数据成员必须具有不可变的常量整型
cout << testClass<int>::d << endl; system("pause");
return ;
}

increment(++)实现(前置式及后置式),dereference(*)实现

i++调用operator++(int), ++i调用operator++()

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class INT
{
friend ostream& operator<<(ostream& os, const INT& i);
public:
INT(int i) :m_i(i) {};
//自增然后得到值
INT& operator++()
{
++(this->m_i);
return *this;
}
//先得到值然后自增
const INT operator++(int)
{
INT temp = *this;
++(this->m_i);
return temp;
}
//取值
int& operator*() const
{
return (int&)m_i;
//下面的语句会出错,因为将int&类型的引用绑定到const int类型的初始值设定项。由于函数是const成员函数导致
//return m_i;
}
private:
int m_i;
}; ostream& operator<<(ostream& os, const INT& i)
{
os << '[' << i.m_i << ']' << endl;
return os;
} int main()
{
INT I();
cout << I++;
cout << ++I;
cout << *I; system("pause");
return ;
}

STL中常用的c++语法的更多相关文章

  1. 仿函数(二、stl中常用仿函数)

    提到C++ STL,首先被人想到的是它的三大组件:Containers, Iterators, Algorithms,即容器,迭代器和算法.容器为用户提供了常用的数据结构,算法大多是独立于容器的常用的 ...

  2. STL中常用容器及操作 学习笔记1

    @[TOC](下面介绍STL中常见的容器及操作)## 不定长数组 vector> vetcor:其实就是一个数组或者说是容器 其操作不同于之前直接定义的数组 > 而且可以直接赋值也可以直接 ...

  3. 编码中常用的SQL语法

    蓝色标注的都是比较常见的SQL ====================== 开发中常见的SQL: left join , right join 防止丢弃数据 inner join CASE WHNE ...

  4. STL中常用算法

    一.排序 sort sort(first_pointer,first_pointer+n,cmp) 默认为升序 若要使用降序,自行写cmp 函数 bool cmp(int a,int b){ retu ...

  5. MATLAB中常用函数及语法

    zeros() 1 zeros(n):n*n 全零矩阵 2 zeros(m,n):m*n全零矩阵 3 zeros(d1,d2,d3……dn):生成 d1*d2*d3*……*dn 全零矩阵或数组. 4 ...

  6. Vue--vue中常用的ECMAScript6语法

    1.对象的写法 es5中对象: {add:add,substrict:substrict} es6中对象: {add,substrict} 注意这种写法的属性名称和值变量是同一个名称才可以简写,否则要 ...

  7. stl中常用的排序算法

    #include"iostream" #include"vector" using namespace std; #include"string&qu ...

  8. C++ STL中的常用容器浅谈

    STL是C/C++开发中一个非常重要的模板,而其中定义的各种容器也是非常方便我们大家使用.下面,我们就浅谈某些常用的容器.这里我们不涉及容器的基本操作之类,只是要讨论一下各个容器其各自的特点.STL中 ...

  9. 深入解析C++ STL中的常用容器

    转载:http://blog.csdn.net/u013443618/article/details/49964299 这里我们不涉及容器的基本操作之类,只是要讨论一下各个容器其各自的特点.STL中的 ...

随机推荐

  1. 对象输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)

    对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流:  Ob ...

  2. bat批处理如何删除本地策略里的用户权限分配中的拒绝从网络访问本机项的guest用户?

    echo [Version]>mm.inf echo signature="$CHICAGO$">>mm.inf echo Revision=1>>m ...

  3. 网页制作常用的CSS知识

    在制作网页中,我们会用到很多CSS的知识,在这里我简单的总结了一些. div    划分区块 ul,li 无序列表(配合划分区块) ol,li 有序列表 a 超链接标签 p 段落标签 h 标题标签 i ...

  4. codeforces_1066_B.Heaters

    题意:一个数组只含有0或1,1表示该元素可以覆盖其自身.左边r-1个元素和右边r-1个元素,问最少保留多少个1元素可以覆盖整个数组. 思路:一个指针指向当前未被覆盖的最左边的元素下标,每次找离它最远且 ...

  5. json 存 window.localStorage.setItem('hideColums',hideArr);

    onColumnSwitch:function(row, $element){ //JSON.parse() var showColumns=$('#table').bootstrapTable('g ...

  6. 实训day01 python基础

    一.编程语言 编程语言:可以被计算机所识别的表达方式. 编程:程序员通过编程语言将自己的想法编写出来,产生的结果就是包含字符的文件. 其中,只有程序在运行时,其中的字符才有特定的语法意义. 二.计算机 ...

  7. go的指针学习

    1)指针是什么? 一个指针变量可以指向任何一个值的内存地址它指向那个值的内存地址 说白了就是可以先存储内存的地址,在用内存地址找到对应值 2)go中的使用 Go 语言的取地址符是 &,放到一个 ...

  8. Python入门之类(class)

    面向对象三大特性 面向对象的三大特性是指:封装.继承和多态. 一.封装 封装,顾名思义就是将内容封装到某个地方,以后再去调用被封装在某处的内容. 所以,在使用面向对象的封装特性时,需要: 将内容封装到 ...

  9. Python之面向对象方法

    Python之面向对象方法 property的用法: property属于类的封装的范畴 property是一种特殊的属性,访问它时会执行一段功能(函数),然后返回值. 用property的方法,就可 ...

  10. leetcode-240搜索二维矩阵II

    搜索二维矩阵II class Solution: def searchMatrix(self, matrix, target): """ :type matrix: Li ...