c++_Template
std::string
Member functions:
Iterators:
-
Return iterator to beginning (public member function)
-
Return iterator to end (public member function)
-
Return reverse iterator to reverse beginning (public member function)
-
Return reverse iterator to reverse end (public member function)
-
Return const_iterator to beginning (public member function)
-
Return const_iterator to end (public member function)
-
Return const_reverse_iterator to reverse beginning (public member function)
-
Return const_reverse_iterator to reverse end (public member function)
Vector
upper_bound in C++(>)
upper_bound() is a standard library function in C++ defined in the header . It returns an iterator pointing to the first element in the range [first, last] that is greater than value, or last if no such element is found.
The elements in the range shall already be sorted or at least partitioned with respect to val.
lower_bound in C++(>=)
The lower_bound() method in C++ is used to return an iterator pointing to the first element in the range [first, last) which has a value not less than val. This means that the function returns an iterator pointing to the next smallest number just greater than or equal to that number. If there are multiple values that are equal to val, lower_bound() returns the iterator of the first such value.
The elements in the range shall already be sorted or at least partitioned with respect to val.
// CPP program to illustrate using
// std :: upper_bound with vectors
#include <bits/stdc++.h>
// Driver code
int main()
{
std::vector<int> v{ 10, 20, 30, 40, 50 };
// Print vector
std::cout << "Vector contains :";
for (int i = 0; i < v.size(); i++)
std::cout << " " << v[i];
std::cout << "\n";
std::vector<int>::iterator upper1, upper2;
// std :: upper_bound
upper1 = std::upper_bound(v.begin(), v.end(), 35);
upper2 = std::upper_bound(v.begin(), v.end(), 45);
std::cout << "\nupper_bound for element 35 is at position : "
<< (upper1 - v.begin());
std::cout << "\nupper_bound for element 45 is at position : "
<< (upper2 - v.begin());
return 0;
}
//Vector contains : 10 20 30 40 50
//upper_bound for element 35 is at position : 3
//upper_bound for element 45 is at position : 4
vector.begin()/rbegin()和find_if( , , )
#include <iostream>
#include <algorithm>
#include "vector"
using namespace std;
int main() {
std::vector<int> v{1,2,3,4,5,6,7,8,9,10};
for(auto i:v){
cout<<i;
}
cout<<endl;
// cout<<"v.end():"<<*(v.end()-1);//10
// cout<<"v.begin():"<<*(v.begin());//1
// cout<<"v.begin()地址:"<<&v[7];//0x1ee535e1b5c
// cout<<"v.begin()地址:"<<*&v[7];//8
//
// cout<<"v.rbegin():"<<*(v.rbegin());//10
// cout<<"v.rend():"<<*(v.rend()-1);//1
cout<<*find_if(v.rbegin(),v.rend(),[](auto it){return it==1;});
// cout<<&find_if(v.rbegin(),v.rend(),[](auto it){return it==1;});
cout<<*find_if(v.rbegin(),v.rend(),[](auto it){return it==1;}).base();
/*擦除从第一个1开始直到最后*/
v.erase(find_if(v.rbegin(),v.rend(),[](auto it){return it==1;}).base(),
v.end());
for(auto i:v){
cout<<i;
}
/*最后只输出了1*/
return 0;
}
Function_1:
vector.begin().rbegin().end().rend()
Function_2:
find_if(InputIterator first, InputIterator last, UnaryPredicate pred)
UnaryPredicate pred(只能接受一个参数|返回Boolean)
UnaryPredicate
is a concept defined in the C++ Standard Library that describes a function or a function object that takes a single argument of any type and returns a Boolean value. The argument type can be explicitly specified or deduced by the compiler.
struct IsEven {
bool operator()(int x) const {
return x % 2 == 0;
}
};
// Usage:
std::vector<int> v = {1, 2, 3, 4, 5};
auto it = std::find_if(v.begin(), v.end(), IsEven{});
operator():the operator()
allows you to treat an object as if it were a function.
class MyFunction{
public:
int operator()(int x){
retutn std::abs(x);
}
}
int main()
{
Myfunction f;
return f(32);
}
以下是 C++ 中经常使用的一些数学方法:(#include "math.h")
- 平方根函数 sqrt():计算给定数的平方根。
- 绝对值函数 abs():计算给定数的绝对值。
- 幂函数 pow():计算给定数的幂。
- 三角函数 sin()、cos()、tan():计算给定角度的正弦、余弦和正切值。
- 对数函数 log()、log10():计算给定数的自然对数和以 10 为底的对数。
- 最大值函数 max():计算给定一组数中的最大值。
- 最小值函数 min():计算给定一组数中的最小值。
- 随机数生成函数 rand():生成一个伪随机数。
- 数值积分函数 integrate():计算给定函数的数值积分。
- 线性代数函数,如解线性方程组、求特征值和特征向量等:这些功能通常需要使用数学库,如 Eigen、GSL 或 Armadillo。
匿名函数/lambda function(可以接受多个参数)
Lambda functions were introduced in C++11 and are a powerful feature that allows developers to write more expressive and readable code. Lambda functions can capture variables from the enclosing scope, have their own parameter list and return type, and can be passed as arguments to other functions or stored in variables.
[capture-list] (parameter-list) -> return-type { function-body }
其中,capture-list
可以是以下三种形式之一:
-
按值捕获:使用
=
指定按值捕获变量。c++Copy codeint a = 10; auto f = [=]() { return a; };
在上述代码中,Lambda 函数
f
按值捕获了变量a
,在函数体内部可以访问变量a
,但不能修改其值。 -
按引用捕获:使用
&
指定按引用捕获变量。c++Copy codeint b = 20; auto g = [&]() { return b; };
在上述代码中,Lambda 函数
g
按引用捕获了变量b
,在函数体内部可以访问并修改变量b
的值。 -
捕获所有变量:使用
&
和=
指定捕获所有变量。c++Copy codeint c = 30; auto h = [&](int x) { return x + a + b + c; };
在上述代码中,Lambda 函数
h
捕获了变量a
、b
、c
,并按引用捕获了变量x
。 -
捕获列表还可以按照变量的类型来捕获变量,如下所示:
c++Copy codeint d = 40; double e = 3.14; auto i = [d, &e]() { return d + e; };
在上述代码中,Lambda 函数 i
按值捕获了变量 d
,按引用捕获了变量 e
,在函数体内部可以访问变量 d
和 e
,但不能修改变量 d
的值。
-
捕获列表还可以为空,此时函数体内部无法访问任何外部变量。例如:
c++Copy code auto j = []() { return 1 + 2; };
在上述代码中,Lambda 函数 j
没有捕获任何变量,函数体内部只能访问常量 1
和 2
。
总之,捕获列表可以根据需要来指定要捕获的变量及其捕获方式,可以灵活地控制函数体内部对外部变量的访问权限,提高了代码的灵活性和可读性。
[](auto it)->bool{return it==1;}
//[] is used to difine a lambda function
cin
c复试2
c++
//
// Created by 20998 on 3/6/2023.
//
#include "iostream"
#include "string"
class Solution{
private:
std::string s;
public:
void input(){
std::getline(std::cin,s);//std::getline可以读入空格,回车停止,把std::cin所读内容放到s处;
std::cin>>s;//读到空格停止,这里会重新读入s
}
void Print(){
std::cout<<s<<std::endl;
}
};
int main()
{
Solution so;
so.input();
so.Print();
printf("0X%x0%d0%o",811,811,811);
return 0;
}
//output:
/*
wo de tian a
wo de tian a
0X32b081101453
*/
标签:std,function,变量,iterator,捕获,c++,Template,函数
From: https://www.cnblogs.com/LogicHan/p/17331391.html