【C++】几个函数的学习

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <vector>
#include <chrono>

int main(int argc, char *argv[]) {
	std::string image_root = argv[1];
	std::string start_end_idx_str = argv[2];
	int pos = start_end_idx_str.find('-');//返回值是字母“-”在母串中的位置(下标记录)
	//如24-67,返回的是2
	int start_idx = 0;
	int end_idx = 0;
	if (pos != std::string::npos) {
		std::string start_idx_str = start_end_idx_str.substr(0, pos);
		//获得字符串start_end_idx_str中从第0位开始的长度为pos的字符串
		std::string end_idx_str = start_end_idx_str.substr(pos + 1);
		start_idx = strtol(start_idx_str.c_str(), NULL, 10);
		end_idx = strtol(end_idx_str.c_str(), NULL, 10);
	}
	else {
		start_idx = 0;
		end_idx = strtol(start_end_idx_str.c_str(), NULL, 10);
	}

	int width = strtol(argv[3], NULL, 10);
	int height = strtol(argv[4], NULL, 10);
	std::string result_root = image_root + "result/";

	std::cout << image_root << ", " << start_idx << " - " << end_idx << std::endl;
	std::cout << width << ", " << height << std::endl;
	std::cout << result_root << std::endl;

	
	return 0;
}

char *argv[] 的用法。

argv[0] = hhn/1/

argv[1] = 46-48

argv[2] = 128

argv[3] = 70

参考:https://www.cnblogs.com/liuzhenbo/p/11044404.html

find

string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos。(返回值可以看成是一个int型的数)

参考:https://www.cnblogs.com/wkfvawl/p/9429128.html

substr

用途:一种构造string的方法

1. 形式:s.substr(pos, n)

2. 解释:返回一个string,包含s中从pos开始的n个字符的拷贝(pos的默认值是0,n的默认值是s.size() - pos,即不加参数会默认拷贝整个s)

3. 补充:若pos的值超过了string的大小,则substr函数会抛出一个out_of_range异常;若pos+n的值超过了string的大小,则substr会调整n的值,只拷贝到string的末尾

头文件:#include<string>

参考:https://www.cnblogs.com/xzxl/p/7243490.html

C++中的C_str()函数用法

用途:c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同. 
这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。

参考:https://blog.csdn.net/Nancy_m/article/details/7583550

strtol用法

用法:long int strtol(const char *nptr, char **endptr, int base)
strtol()会将nptr指向的字符串,根据参数base,按权转化为long int, 然后返回这个值。

参考:https://blog.csdn.net/zxx2096/article/details/81127858

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值