c 11 筆記,c 筆記

2021-06-20 01:49:36 字數 2285 閱讀 9932

find_if的使用

bool isthe(const string&s1)

vectora=;

auto aaa=find_if(a.begin(),a.end(),isthe);

aaa為第乙個符合要求的位址。*aaa為取到的值。(aaa為迭代器)

lambda 表示式

auto f=;

cout<

auto aaa=find_if(a.begin(),a.end(),isthe);

等價於

auto aaa=find_if(a.begin(),a.end(),(const string &s1));

auto f=(const int &a ,const int & b);

cout <

使用佔位符:需要新增下面標頭檔案

using namespace std;

using std::placeholders::_1;

經常碰到字串分割的問題,這裡總結下,也方便我以後使用。

一、用strtok

函式進行字串分割

原型: char *strtok(char *str, const char *delim);

功能:分解字串為一組字串。

引數說明:str

為要分解的字串,

delim

為分隔符字串。

返回值:從s

tr開頭開始的乙個個被分割的串。當沒有被分割的串時則返回

null

。示例:

//借助strtok實現split

二、用stl

進行字串的分割

涉及到string類的兩個函式find和substr:

1、find函式

原型:size_t find ( const string& str, size_t pos = 0 ) const;

功能:查詢子字串第一次出現的位置。

引數說明:str為子字串,pos為初始查詢位置。

返回值:找到的話返回第一次出現的位置,否則返回string::npos 

2、substr函式

原型:string substr ( size_t pos = 0, size_t n = npos ) const;

功能:獲得子字串。

引數說明:pos為起始位置(預設為0),n為結束位置(預設為npos)

返回值:子字串 

//字串分割函式

std::vectorsplit(std::string str,std::string pattern)

{ std::string::size_type pos;

std::vectorresult;

str+=pattern;//擴充套件字串以方便操作

int size=str.size();

for(int i=0; i

#include #include #include //字串分割函式

std::vectorsplit(std::string str,std::string pattern)

{ std::string::size_type pos;

std::vectorresult;

str+=pattern;//擴充套件字串以方便操作

int size=str.size();

for(int i=0; i>str;

getline(std::cin,str);

std::string pattern;

std::cout

getline(std::cin,pattern);//用於獲取含空格的字串

std::vectorresult=split(str,pattern);

std::cout<

深入應用C 11 筆記(四)

1.8 tupe元組 構造tuple 標頭檔案為 使用make tuple 構造乙個tuple char sendpack int nsendsize tuple char int tp make tuple sendpack,nsendsize 使用std tie構造 tuple char int...

深入理解C 11 筆記

include using namespace std classa a 對於含有堆記憶體的類,需要提供深拷貝的拷貝建構函式,避免預設的拷貝構造使用淺拷貝導致堆記憶體的重複刪除。a const a a m ptr new int a.m ptr 通過移動構造,a 作為函式引數,只使用淺拷貝避免臨時物...

《深入理解C 11》筆記 decltype

本篇將介紹decltype的用法。decltype與auto類似,也能進行型別推導,但是用法有一定的區別,decltype推導出的型別能作為型別宣告變數 int main decltype的應用 一種是decltype和typedef using的合用 using size t decltype s...