-
CPP允许将变量定义成数组的引用
void print(int(&arr)[10]) { for (auto elem : arr) { std::cout << elem << std::endl; } }
-
CPP的initializer_list允许构建一个类似于CS的param数组,但是要求数组类型相同
#include <iostream> #include <climits> #include <vector> #include <iterator> #include <string> #include <initializer_list> template<typename Type> void print(std::string, std::initializer_list<Type> list) { std::initializer_list<int> lst1{ 1,2,3 }; std::initializer_list<int> lst2(lst1); lst2 = lst1; std::cout << lst1.size() << std::endl; for (std::initializer_list<Type>::iterator it = list.begin() it != list.end(); ++it) { std::cout << *it << " "; } std::cout << std::endl; } int main() { int arr[10] = { 0,1,2,3,4,5,6,7,8,9 }; print("sdfsdf", { 2, 3, 4, 5, 6 }); //print({ 2.0,3,4.0 }); X return 0; }
-
CPP可以用列表初始化返回值。
#include <iostream> #include <climits> #include <vector> #include <iterator> #include <string> #include <initializer_list> std::vector<std::string>process(const std::string& expected, const std::string& actual) { if (expected.empty()) return {}; else if (expected == actual) return { "func", "okay" }; else return { "func", expected, actual }; } int main() { std::string str1, str2("123"), str3{ "234" }; std::cout << process(str1, str1).size() << std::endl; for (auto x : process(str2, str2)) { std::cout << x << " "; } std::cout << std::endl; for (auto x : process(str2, str3)) { std::cout << x << " "; } return 0; }
-
CPP可以使用一下两种方式创建一个返回数组指针的函数,其中第一个方式叫尾指返回类型声明,可以用这个方法返回函数指针。
#include <iostream> #include <climits> #include <vector> #include <iterator> #include <string> #include <initializer_list> auto func1(int i) -> int(*)[10] { static int arr[10]{ 1,2,3,4,5,6,7,8,9,10 }; return &arr; } auto (*func2(int i))[10] { static int arr[10]{ 1,2,3,4,5,6,7,8,9,10 }; return &arr; } auto func3(int i) -> int(*)(int*, int); int main() { auto arr = func1(1); auto brr = func2(1); for (int i = 1; i < 10; ++i) { std::cout << (*arr)[i] << " "; } std::cout << std::endl; for (int i = 1; i < 10; ++i) { std::cout << (*brr)[i] << " "; } return 0; }
-
预处理器定义的一些有用的字符串或者宏定义。
#include <iostream> #include <climits> #include <vector> #include <iterator> #include <string> #include <initializer_list> #include <cassert> void print(size_t size) { #ifndef NDEBUG std::cout << "Function Name: " << __func__ << std::endl; //打印所在的函数名 std::cout << "File name: " << __FILE__ << std::endl; //打印当前行号 std::cout << "Line name: " << __LINE__ << std::endl; //打印当前时分秒 std::cout << "Time name: " << __TIME__ << std::endl; //打印当前日期 std::cout << "Date name: " << __DATE__ << std::endl; #endif } int main() { int i = 1; print(1); return 0; }
-
函数指针注意一下代码:
#include <iostream> #include <climits> #include <vector> #include <iterator> #include <string> #include <initializer_list> #include <cassert> bool LenthCompare(const std::string& str1, const std::string& str2) { return str1.size() < str2.size(); } int main() { bool (*pf)(const std::string&, const std::string&); decltype(LenthCompare) *pf2(const std::string& str1, const std::string& str2); pf = LenthCompare; //相当于下面的语句 //pf = &LenthCompare; bool b1 = pf("hello", "goodbye"); bool b2 = (*pf)("hello", "goodbye"); bool b3 = LenthCompare("hello", "goodbye"); //上面三条语句的意思是一样的 std::cout << b1 << " " << b2 << " " << b3 << std::endl; pf = nullptr; pf = 0; //函数指针可以指向0或者空 return 0; }
1 1 1
-
函数指针可以传参数和起别名:
#include <iostream> #include <climits> #include <vector> #include <iterator> #include <string> #include <initializer_list> #include <cassert> void UseBigger(const std::string& str1, const std::string& str2, bool pf(const std::string&, const std::string&)) { } bool LenghCompare(const std::string& str1, const std::string& str2) { return true; } //上面的函数原型等于下面的函数原型 /*void UseBigger(const std::string& str1, const std::string& str2, bool (*pf)(const std::string&, const std::string&));*/ typedef bool Func1(const std::string&, const std::string&); typedef decltype(LenghCompare) Func2; typedef bool (*Func3)(const std::string&, const std::string&); typedef decltype(LenghCompare)* Func4; using Func5 = bool(*)(const std::string&, const std::string&); //上面五种类型起别名的效果一样 int main() { //可以把函数名直接当函数指针传递参数 UseBigger("234", "@34", LenghCompare); return 0; }
-
默认构造函数会将类内的值初始化为0或者空,如果类内存在初始值,则将值初始化为该值,如果想定义默认构造函数,可以写出默认函数原型并且在原型后面加=default。通过初始化列表的方式定义构造函数,如果列表没有初始化部分变量,则该变量会像默认构造函数初始化变量一样,被赋值。如果一个类内的变量被mutable修饰,则该变量无论在什么情况下都可以被改变,即使对象被const修饰
#include <iostream> #include <climits> #include <vector> #include <iterator> #include <string> #include <initializer_list> #include <cassert> #include <algorithm> class Base { public: int a = 0; int b = 1; mutable int c = 2; Base() = default; Base(int x, int y) :a(x), b(y) {}; void func() const { c = 3; } }; int main() { Base b(1, 2); std::cout << b.a << " " << b.b << " " << b.c << std::endl; b.func(); std::cout << b.a << " " << b.b << " " << b.c << std::endl; }
1 2 2 1 2 3
文章来源地址https://www.toymoban.com/news/detail-728232.html
文章来源:https://www.toymoban.com/news/detail-728232.html
到了这里,关于cpp primer笔记030-参数传递的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!