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

浅谈c++中map插入数据的用法

map:数据的插入

在构造map容器后,我们就可以往里面插入数据了。这里讲三种插入数据的方法:
第一种:用insert函数插入pair数据

 map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1,“student_one”));
第二种:用insert函数插入value_type数据

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的更多相关文章

  1. C++常见问题: 字符串分割函数 split

    C++标准库里面没有字符分割函数split ,这可太不方便了,我已经遇到>3次如何对字符串快速分割这个问题了.列几个常用方法以备不时之需. 方法一: 利用STL自己实现split 函数(常用,简 ...

  2. JavaScript中字符串分割函数split用法实例

    这篇文章主要介绍了JavaScript中字符串分割函数split用法,实例分析了javascript中split函数操作字符串的技巧,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了JavaSc ...

  3. Delphi 自带的字符串分割函数split

    下面介绍Delphi自带的字符串分割函数,根据你的需要来使用. 1.ExtractStrings function ExtractStrings(Separators, WhiteSpace: TSy ...

  4. C++之字符串分割函数split

    c++之字符串分割: /* *c++之字符串分割: */ #include <iostream> #include <string> #include <vector&g ...

  5. Java字符串分割函数split源码分析

    spilt方法作用 以所有匹配regex的子串为分隔符,将input划分为多个子串. 例如: The input "boo:and:foo", for example, yield ...

  6. SQL Server自定义字符串分割函数——Split

    我相信大部分人都碰到过,处理数据的时候,字段的值是以 ',' (逗号)分隔的形式,所以我也不能避免. 然后我才知道,sql 是没有类似于 C# 和 Javascript 这种分割字符串的方法.( Sp ...

  7. hive函数 -- split 字符串分割函数

    hive字符串分割函数 split(str, regex) - Splits str around occurances that match regexTime taken: 0.769 secon ...

  8. Split字符串分割函数

    非常非常常用的一个函数Split字符串分割函数. Dim myTest myTest = "aaa/bbb/ccc/ddd/eee/fff/ggg" Dim arrTest arr ...

  9. ASP.NET中常用的字符串分割函数

    asp.net字符串分割函数用法 先来看个简单的实例 但是其数组长度却是25,而不是3.下面这种方法是先将“[111cn.net]”替换成一个特殊字符,比如$,在根据这个字符执行Split 例如下面我 ...

随机推荐

  1. TCP的三次握手(建立连接)和四次挥手(关闭连接)(转)

    转自:(http://www.cnblogs.com/Jessy/p/3535612.html) 参照: http://course.ccniit.com/CSTD/Linux/reference/f ...

  2. Java线上应用故障排查之一:高CPU占用 (转)

    一个应用占用CPU很高,除了确实是计算密集型应用之外,通常原因都是出现了死循环. (友情提示:本博文章欢迎转载,但请注明出处:hankchen,http://www.blogjava.net/hank ...

  3. [leetcode-884-Uncommon Words from Two Sentences]

    We are given two sentences A and B.  (A sentence is a string of space separated words.  Each word co ...

  4. Scrum立会报告+燃尽图(十月二十七日总第十八次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2246 项目地址:https://git.coding.net/zhang ...

  5. mysql 多查询临时表的运用

    SELECT * from (select count(*) imgCount1 from imagetable where SeriesID = '1201061992020630292018092 ...

  6. OpenFlow协议

    功能 1.0版本Openflow:控制器通过Openflow协议与交换机建立了安全通道(Sceure Channel),下发流表. 1.3版本Openflow:多控制器,多流表. 用于实现Contro ...

  7. 团队Alpha冲刺(三)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:家伟 组员8:政演 组员9:鸿杰 组员10:刘一好 组员11:何宇恒 展示组内最 ...

  8. Redis中的GETBIT和SETBIT(转载)

    Redis是in-memery的数据库,其优势不言而喻.详细可以阅读一下官网的介绍.https://redis.io 其主要有五种数据类型:strings,lists,sets,hashes.在学习到 ...

  9. EF动态排序

    转载的代码,改天再研究 public PageData<T> FindAll(int PageIndex, int PageSize, Expression<Func<T, b ...

  10. css选择器和新增UI样式总结

    经过两天的学习,初步对css3选择器和新增UI样式有了进一步的理解.