(转)C++常见问题: 字符串分割函数 split
http://www.cnblogs.com/dfcao/p/cpp-FAQ-split.html
C++标准库里面没有字符分割函数split ,这可太不方便了,我已经遇到>3次如何对字符串快速分割这个问题了。列几个常用方法以备不时之需。
方法一: 利用STL自己实现split 函数(常用,简单,直观)
原型: vector<string> split(const string &s, const string &seperator);
输入一个字符串,一个分隔符字符串(可包含多个分隔符),返回一个字符串向量。这是我最喜欢的方法,因为它最直观,在平常也最常用。实现及测试代码如下
#include <vector>
#include <string>
#include <iostream>
using namespace std; vector<string> split(const string &s, const string &seperator){
vector<string> result;
typedef string::size_type string_size;
string_size i = ; while(i != s.size()){
//找到字符串中首个不等于分隔符的字母;
int flag = ;
while(i != s.size() && flag == ){
flag = ;
for(string_size x = ; x < seperator.size(); ++x)
if(s[i] == seperator[x]){
++i;
flag = ;
break;
}
} //找到又一个分隔符,将两个分隔符之间的字符串取出;
flag = ;
string_size j = i;
while(j != s.size() && flag == ){
for(string_size x = ; x < seperator.size(); ++x)
if(s[j] == seperator[x]){
flag = ;
break;
}
if(flag == )
++j;
}
if(i != j){
result.push_back(s.substr(i, j-i));
i = j;
}
}
return result;
} int main(){
string s = "a,b*c*d,e";
vector<string> v = split(s, ",*"); //可按多个字符来分隔;
for(vector<string>::size_type i = ; i != v.size(); ++i)
cout << v[i] << " ";
cout << endl;
//输出: a b c d
}
提供了一段更简洁高效的代码,实现如下:
void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c)
{
std::string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = ;
while(std::string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2-pos1)); pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if(pos1 != s.length())
v.push_back(s.substr(pos1));
}
方法二: 用C语言中的strtok 函数来进行分割
原型: char *strtok(char *str, const char *delim);
strtok函数包含在头文件<string.h>中,对于字符数组可以采用这种方法处理。当然也可以将字符数组转换成字符串之后再使用法一。测试代码如下
#include <string.h>
#include <stdio.h> int main(){
char s[] = "a,b*c,d";
const char *sep = ",*"; //可按多个字符来分割
char *p;
p = strtok(s, sep);
while(p){
printf("%s ", p);
p = strtok(NULL, sep);
}
printf("\n");
return ;
}
//输出: a b c d
map:数据的插入
第一种:用insert函数插入pair数据
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1,“student_one”));
map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type (1,"student_one"));
mapStudent.insert(make_pair(1, "student_one"));
第三种:用数组方式插入数据
map<int, string> mapStudent;
mapStudent[1] = “student_one”;
mapStudent[2] = “student_two”;
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是不能再插入这个数据的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,即:如果当前存在该关键字,则覆盖改关键字的值,否则,以改关键字新建一个key—value;
笔试题代码:
#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std; #define HTTP_HEADER \
"GET /index.html HTTP/1.1.\r\n" \
"Content-Type :text\r\n" \
"Host:web\r\n" \
"Accept:text/thml\r\n" \
"Accept-Encoding:identifty\r\n" \
"User-Agent:Morzilla\r\n" \
"\r\n" vector<string> split(const string &s, const string &seperator){
vector<string> result;
typedef string::size_type string_size;
string_size i = ; while (i != s.size()){
//找到字符串中首个不等于分隔符的字母;
int flag = ;
while (i != s.size() && flag == ){
flag = ;
for (string_size x = ; x < seperator.size(); ++x)
if (s[i] == seperator[x]){
++i;
flag = ;
break;
}
} //找到又一个分隔符,将两个分隔符之间的字符串取出;
flag = ;
string_size j = i;
while (j != s.size() && flag == ){
for (string_size x = ; x < seperator.size(); ++x)
if (s[j] == seperator[x]){
flag = ;
break;
}
if (flag == )
++j;
}
if (i != j){
result.push_back(s.substr(i, j - i));
i = j;
}
}
return result;
} void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c)
{
std::string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = ;
while (std::string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2 - pos1)); pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if (pos1 != s.length())
v.push_back(s.substr(pos1));
} int main(){ map<string, string> headers;
string str = HTTP_HEADER;
cout << str << endl;
vector<string> v = split(str, "\r\n"); //可按多个字符来分隔; string str0Line = v[];
string str1Line = v[];
string str2Line = v[];
string str3Line = v[];
string str4Line = v[];
string str5Line = v[]; cout << str0Line << endl;
cout << str1Line << endl; cout << str2Line << endl; cout << str3Line << endl; cout << str4Line << endl; cout << str5Line << endl; vector<string> v1 = split(str0Line, " "); //可按多个字符来分隔;
string str00Line = v1[];
string str01Line = v1[];
string str02Line = v1[];
cout << str00Line << endl;
cout << str01Line << endl;
cout << str02Line << endl; for (vector<string>::size_type i = ; i != v.size(); ++i)
{
vector<string> v2 = split(v[i], ":"); //可按:字符来分隔;
string str10Line = v2[];
string str11Line = v2[]; headers.insert(pair<string, string>(str10Line, str11Line));
cout << str10Line << " " << str11Line << endl;
} getchar(); getchar();
return ;
}
(转)C++常见问题: 字符串分割函数 split的更多相关文章
- C++常见问题: 字符串分割函数 split
C++标准库里面没有字符分割函数split ,这可太不方便了,我已经遇到>3次如何对字符串快速分割这个问题了.列几个常用方法以备不时之需. 方法一: 利用STL自己实现split 函数(常用,简 ...
- JavaScript中字符串分割函数split用法实例
这篇文章主要介绍了JavaScript中字符串分割函数split用法,实例分析了javascript中split函数操作字符串的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了JavaSc ...
- Delphi 自带的字符串分割函数split
下面介绍Delphi自带的字符串分割函数,根据你的需要来使用. 1.ExtractStrings function ExtractStrings(Separators, WhiteSpace: TSy ...
- C++之字符串分割函数split
c++之字符串分割: /* *c++之字符串分割: */ #include <iostream> #include <string> #include <vector&g ...
- Java字符串分割函数split源码分析
spilt方法作用 以所有匹配regex的子串为分隔符,将input划分为多个子串. 例如: The input "boo:and:foo", for example, yield ...
- SQL Server自定义字符串分割函数——Split
我相信大部分人都碰到过,处理数据的时候,字段的值是以 ',' (逗号)分隔的形式,所以我也不能避免. 然后我才知道,sql 是没有类似于 C# 和 Javascript 这种分割字符串的方法.( Sp ...
- hive函数 -- split 字符串分割函数
hive字符串分割函数 split(str, regex) - Splits str around occurances that match regexTime taken: 0.769 secon ...
- Split字符串分割函数
非常非常常用的一个函数Split字符串分割函数. Dim myTest myTest = "aaa/bbb/ccc/ddd/eee/fff/ggg" Dim arrTest arr ...
- ASP.NET中常用的字符串分割函数
asp.net字符串分割函数用法 先来看个简单的实例 但是其数组长度却是25,而不是3.下面这种方法是先将“[111cn.net]”替换成一个特殊字符,比如$,在根据这个字符执行Split 例如下面我 ...
随机推荐
- “Hello World!”团队召开的第六次会议
团队“Hello World!”团队召开的第六次会议. 博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.Todo List 六.会议照片 七.燃尽图 一.会议时间 2017年1 ...
- 周总结<6>
周次 学习时间 新编写代码行数 博客量(篇) 学到知识点 13 10 100 2 网页设计:邻接矩阵深度以及广度遍历
- Java中按值传递与按引用传递的区别
值传递:(形式参数类型是基本数据类型):方法调用时,实际参数把它的值传递给对应的形式参数,形式参数只是用实际参数的值初始化自己的存储单元内容,是两个不同的存储单元,所以方法执行中形式参数值的改变不影响 ...
- 03_Java基础语法_第3天(Scanner、Random、流程控制语句)_讲义
今日内容介绍 1.引用类型变量的创建及使用 2.流程控制语句之选择语句 3.流程控制语句之循环语句 4.循环高级 01创建引用类型变量公式 * A: 创建引用类型变量公式 * a: 我们要学的Scan ...
- 多tab点击切换
现在来一个小练习,就是用js实现多tab之间的切换: <body> <ul id="tab"> <li id="tab1"> ...
- 基于opencv的小波变换代码和图像结果
#include "stdafx.h" #include "WaveTransform.h" #include <math.h> #include ...
- js & array to string
js & array to string https://stackoverflow.com/questions/13272406/convert-string-with-commas-to- ...
- 图解用HTML5的popstate如何玩转浏览器历史记录
一.popstate用来做什么的?简而言之就是HTML5新增的用来控制浏览器历史记录的api. 二.过去如何操纵浏览器历史记录? window.history对象,该对象上包含有length和stat ...
- Spring boot整合shiro框架(2)
form提交 <form th:action="@{/login}" method="POST"> <div class="form ...
- Collection接口框架
1. Collection接口 其主要的UML类图: Collection接口继承自Iterable接口.Iterable接口中定义了Iterable方法,该方法会返回一个迭代器,用于遍历合集中的元素 ...