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. MathExam第二次作业(升级版)

    MathExamLv2——林华伟 211506319 陈珍 211406263   一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实 ...

  2. javascript方法扩展

    String.prototype.startWith = function(str){ return str.indexOf(str) == 0; }; var str = "abc&quo ...

  3. Java中的网络编程-3

    用户数据协议(UDP)是网络信息传输的另外一种形式, 基于UDP的通信不同于基于TCP的通信, 基于UDP的信息传递更快, 但是不提供可靠的保证. 使用UDP传输数据时, 用户无法知道数据能否正确地到 ...

  4. 移动平台的meta标签

    这个meta在移动平台上有非常神奇的地方. 1. <meta name="viewport" content="width=device-width; initia ...

  5. Oracle win64_12g 安装

    1.Oracle win64_12g 安装 1.下载安装包:这里需要自己注册一下,然后就可以登录下载软件了. 下载地址: http://www.oracle.com/technetwork/datab ...

  6. linux设置时区和自动同步时间

    1.设置时区 编辑 /etc/sysconfig/clock 修改 ZONE="Asia/Shanghai" 然后  cp  /usr/share/zoneinfo/Asia/Sh ...

  7. word批量转pdf文件快捷方法。

    最近在工作中因为要遇到大量的Word文件转化为PDF文件来实现平台的迁移.但是由于文件太多,手动很费力,想到了用代码的方式: 复制下面的代码,保存的记事本,另存为vbs文件:然后把这个vbs文件放到你 ...

  8. jenkin报错hudson.plugins.git.GitExcept

    清除工作空间 转载请注明博客出处:http://www.cnblogs.com/cjh-notes/

  9. Linux 下安装 java 环境(jdk + mysql + tomcat)

    Linux选用的是 centOS 6.8 64位 ,最先要将 centOS 中自带的 jdk 和 myqsql 卸载掉. 首先安装 了 SSH,通过 SSH 将 jdk,mysql,tomcat 的压 ...

  10. BZOJ 1216 操作系统(堆)

    用堆模拟题目中的操作即可. # include <cstdio> # include <cstring> # include <cstdlib> # include ...