To support easy adoption of RAII principles, the C++ Standard Library provides three smart pointer types:
Here are some important examples:
for_each
, the default traversal algorithm (along with range-based for loops).transform
, for not-in-place modification of container elementsfind_if
, the default search algorithm.sort
, lower_bound, and the other default sorting and searching algorithms.auto comp = [](const widget& w1, const widget& w2)
{ return w1.weight() < w2.weight(); }
sort( v.begin(), v.end(), comp );
auto i = lower_bound( v.begin(), v.end(), comp );
int value {5};
auto iter1 = std::find_if(std::begin(numbers), std::end(numbers),[value](int n) { return n > value; });
if(iter1 != std::end(numbers))
std::cout << *iter1 << " was found greater than " << value << ".\n";
如op的一个实现 即将[first1, last1]范围内的每个元素加5,然后依次存储到result中。
int op_increase(int i) {return (i + 5)};
调用std::transform的方式如下:
std::transform(first1, last1, result, op_increase);
#include <iostream>
#include <vector>
int main(){
std::vector<int> v {1,2,3};
// C-style
for(int i = 0; i < v.size(); ++i){
std::cout << v[i];
}
// Modern C++:
for(auto x : v) std::cout << x;
for(auto& x : v) ++x; // 使用引用,方便我们修改容器中的数据
}
#define SIZE 10 // C-style
constexpr int size = 10; // modern C++
C++11的基本思想是,允许非静态(non-static)数据成员在其声明处(在其所属类内部)进行初始化。这样,在运行时,需要初始值时构造函数可以使用这个初始值(解决了C++0x之前非常蹩脚的一个初始化形式,尤其对 const char*
和 C类型数组)。
class A {
public:
int a = 7;
};
在现代C ++中,可以对任何类型使用括号初始化。当初始化数组,向量或其他容器时,这种形式的初始化特别方便。在以下示例中,v2使用的三个实例初始化S。v3用三个实例初始化,而这三个实例S本身都用花括号初始化。编译器根据的声明类型推断每个元素的类型v3。
#include <vector>
struct S{
std::string name;
float num;
S(std::string s, float f) : name(s), num(f) {}
};
int main(){
// C-style initialization
std::vector<S> v;
S s1("Norah", 2.7);
S s2("Frank", 3.5);
S s3("Jeri", 85.9);
v.push_back(s1);
v.push_back(s2);
v.push_back(s3);
// Modern C++:
std::vector<S> v2 {s1, s2, s3};
// or...
std::vector<S> v3{ {"Norah", 2.7}, {"Frank", 3.5}, {"Jeri", 85.9} };
}
在c++中初始化对象的一种方法是聚合初始化,它允许使用花括号从多个值初始化:
struct Data
{
std::string name;
double value;
};
Data x{"test1", 6.778};
在c++ 17中聚合可以有基类,所以对于从其他类/结构派生的结构,允许初始化列表:
struct MoreData : Data
{
bool done;
};
MoreData y{{"test1", 6.778}, false};
std::vector<int> v {1,2,3,4,5};
int x = 2;
int y = 4;
auto result = find_if(begin(v), end(v), [=](int i) { return i > x && i < y; });
c++ 17还引入了一个新的类型 std::is_aggregate
来测试一个类型是否是一个聚合:
template<typename T>
struct D : std::string, std::complex<T>
{
std::string data;
};
D<float> s{{"hello"}, {4.5,6.7}, "world"}; // OK since C++17
std::cout << std::is_aggregate<decltype(s)>::value; // outputs: 1 (true)
#include <stdexcept>
#include <limits>
#include <iostream>
using namespace std;
void MyFunc(int c){
if (c > numeric_limits<char> ::max())
throw invalid_argument("MyFunc argument too large.");
//...
}
int main(){
try{
MyFunc(256); //cause an exception to throw
}catch (invalid_argument& e){
cerr << e.what() << endl;
return -1;
}
//...
return 0;
}
强大的错误处理对于任何编程语言都很有挑战性。 尽管异常提供了多个支持良好错误处理的功能,但它们无法为你完成所有工作。 若要实现异常机制的优点,请在设计代码时记住异常。
noexcept
的部分。Use the C++ Standard Library std::atomic
struct and related types for inter-thread communication mechanisms.
原文:https://www.cnblogs.com/brt2/p/12975205.html