python中的configparser模块
configparser可以方便的读取配置文件
比如有一个config.ini
[user] user_name = Mr,X password = 222 [connect] ip = 127.0.0.1 port = 4723
程序代码
import configparser config = configparser.ConfigParser() filename = 'config.ini' config.read(filename, encoding='utf-8') ConfigParserObject.sections() ConfigParserObject.items(section) ConfigParserObject.options(section) ConfigParserObject.get(section, option) ConfigParserObject.getint(section, option) ConfigParserObject.getboolean(section, option) ConfigParserObject.getfloat(section, option) ConfigParserObject.has_section(section) ConfigParserObject.has_option(section, option) ConfigParserObject.add_section(section) ConfigParserObject.set(section, option, value) ConfigParserObject.remove_section(section) ConfigParserObject.remove_option(section, option) ConfigParserObject.write(open(filename, 'w'))
python中的argparse模块
argparse模块方便命令行调用
import argparse parser = argparse.ArgumentParser(description='Running DeepSpeech inference.') parser.add_argument('--model', required=True, help='Path to the model (protocol buffer binary file)') parser.add_argument('--candidate_transcripts', type=int, default=3, help='Number of candidate transcripts to include in JSON output') args = parser.parse_args()
程序运行内存区域
代码区,存储二进制代码,无法更改。
栈区,由编译器自动分配释放,存储函数参数,局部变量的值。
堆区,由程序代码分配释放,new delete等。
常量区,存储常量,包括字面值常量等。
全局区,存储全局变量和static变量等,分为初始化区和未初始化区。
c++中的partition()函数
在序列中分区元素会重新对元素进行排列,所有使给定谓词返回 true 的元素会被放在所有使谓词返回 false 的元素的前面。这就是 partition() 算法所做的事。
示例,leetcode上剑指offer21题,输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分。
class Solution { public: vector<int> exchange(vector<int>& nums) { partition(nums.begin(), nums.end(), [](int &n){ return n&1; }); return nums; } };
c++匿名函数的写法
[captures] (params) -> ret {Statments;}
captures是外部变量截取,params是函数参数,ret是返回值类型,statments则是语句。
下面是各种变量截取的选项:
[] 不截取任何变量
[&} 截取外部作用域中所有变量,并作为引用在函数体中使用
[=] 截取外部作用域中所有变量,并拷贝一份在函数体中使用
[=, &foo] 截取外部作用域中所有变量,并拷贝一份在函数体中使用,但是对foo变量使用引用
[bar] 截取bar变量并且拷贝一份在函数体重使用,同时不截取其他变量
[this] 截取当前类中的this指针。如果已经使用了&或者=就默认添加此选项。
匿名函数可以配合for_each使用,比如for_each( v.begin(), v.end(), [] (int val) { cout << val; } );