杂项
来源:互联网 发布:java项目开发流程图 编辑:程序博客网 时间:2024/05/21 19:39
常用拼接:
char* + int
转换为char*
char* str = "hello";
int a = 10;
char *buffer = new char[strlen(str)+sizeof(a)+1];
sprintf(buffer, "_%s_%d_", str, a);
cout << buffer << endl;
转换为string
string s = "hello";
int a = 10;
s += to_string(a);
cout << s << endl;
转换为string(利用字符串io流)
string s = "hello";
int a = 10;
ostringstream ss;
ss << s << a << endl;
s = ss.str();
cout << s << endl;
常用拆分:
void
TestStrtok()
{
//1.非线程安全的,如果多个线程同时调用,会覆盖掉原来的值.
//2.支持以字符串分割.
//3.源字符串必须是可修改的.
char
c_str[]=
"google||twitter||facebook||microsoft||apple||ibm||"
;
const
char
* delim =
"||"
;
char
* result = strtok(c_str,delim);
while
(result != NULL)
{
cout << result << endl;
result = strtok(NULL,delim);
}
}
void
TestGetLineWithStringStream()
{
//1.线程安全的,但是只能以字符作为分隔符
stringstream ss(
"google|twitter|facebook|microsoft|apple|ibm|"
);
string str;
while
(getline(ss,str,
'|'
))
{
cout << str << endl;
}
}
void
TestStringFind()
{
//1.自己实现,线程安全,支持字符串作为分隔符.缺点可能就是代码量多.
string str =
"google||twitter||facebook||microsoft||apple||ibm||"
;
const
char
* delim =
"||"
;
const
int
len = strlen(delim);
size_t index =
0
;
size_t pos = str.find(delim,index);
while
(pos != string::npos)
{
string ss = str.substr(index,pos-index);
cout << ss << endl;
index = pos+len;
pos = str.find(delim,index);
}
//cout << "is last?" << " index:" << index << " str.length():" << str.length() << endl;
if
((index+
1
) < str.length())
{
string ss = str.substr(index,str.length() - index);
cout << ss << endl;
}
}
阅读全文
0 0
- 杂项
- 杂项
- 杂项
- 杂项
- 杂项
- 杂项
- 杂项
- 杂项
- 杂项
- 杂项
- 杂项
- 杂项
- 杂项
- 杂项
- 杂项
- 杂项
- 杂项
- 杂项
- java中的组合关系
- 【Dubbo分布式服务框架】5.传统测试工程的搭建1
- (27)模块方法模式(理解抽象、继承和final)
- 安卓开发点击CheckBox显示明文密码
- 洛谷 P3478 [POI2008]STA-Station(bzoj P1131 [POI2008]STA-Station)
- 杂项
- 计算机位运算
- java并发编程实战-基础构建模块3
- C 语言返璞归真: 指针篇番外
- EJB到底是什么?
- 用户注册
- sklearn.linear_model.LinearRegression
- PYTHON数据分析入门
- Educational Codeforces Round 22 C The Tag Game(树的深度)