C++ split分割字符串函数
将字符串绑定到输入流istringstream,然后使用getline的第三个参数,自定义使用什么符号进行分割就可以了。
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
void split(const string& s,vector<int>& sv,const char flag = ' ') {
sv.clear();
istringstream iss(s);
string temp; while (getline(iss, temp, flag)) {
sv.push_back(stoi(temp));
}
return;
} int main() {
string s("123:456:7");
vector<int> sv;
split(s, sv, ':');
for (const auto& s : sv) {
cout << s << endl;
}
system("pause");
return ;
}
2、使用strtok函数。
strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delim中包含的分割字符时,则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针。
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char sentence[]="This is a sentence with 7 tokens";
cout << "The string to be tokenized is:\n" << sentence << "\n\nThe tokens are:\n\n";
char *tokenPtr=strtok(sentence," ");//sentence必须是一个char数组,不能是定义成指针形式
while(tokenPtr!=NULL) {
cout<<tokenPtr<<'\n';
tokenPtr=strtok(NULL," ");
}
//cout << "After strtok,sentence=" << tokenPtr<<endl;
return ;
} //对于string s;
//char tar[10000];
//strcpy(tar,s.c_str());
C++ split分割字符串函数的更多相关文章
- Sql Server分割字符串函数
-- Description: 分割字符串函数 -- SELECT * FROM dbo.Split('a,b,c,d,e,f,g',',') -- ========================= ...
- PLSQL Split分割字符串
系统自带的split,使用起来方便,但是如果字符串太长,可能会出现异常,这里,我自己写了一个也是该名字,放在自己的包中,引用的时候带包名就好了. --系统自带的函数 /*CURSOR cur_temp ...
- Replace是替代 Split分割字符串
Replace是替代 Split分割字符串string[] ReadText = str.Replace("\r\n", "@").Split('@'); Sp ...
- C# Split的用法,Split分割字符串
C# Split的用法,Split分割字符串 分割单个字串:string str="来自张三的亲切问候!;string[] strarry=str.Split(new string[] { ...
- C#的String.Split 分割字符串用法详解的代码
代码期间,把代码过程经常用的内容做个珍藏,下边代码是关于C#的String.Split 分割字符串用法详解的代码,应该对码农们有些用途. 1) public string[] Split(params ...
- java关于split分割字符串,空的字符串不能得到的问题
java关于split分割字符串,空的字符串不能得到的问题 class T { public static void main(String args[]) { String num[] = ne ...
- SQLServer实现split分割字符串到列
网上已有人实现sqlserver的split函数可将字符串分割成行,但是我们习惯了split返回数组或者列表,因此这里对其做一些改动,最终实现也许不尽如意,但是也能解决一些问题. 先贴上某大牛写的sp ...
- C#中使用split分割字符串的几种方法小结
1.用字符串分隔: using System.Text.RegularExpressions;string str="aaajsbbbjsccc";string[] sArray= ...
- String.split()分割字符串方法
split方法的主要用处就是:分割字符串 split方法返回的是数组类型 主要由以下几种用法: 1.比如有一个字符串var str = "bcadeab";对str使用split方 ...
随机推荐
- Linux虚拟文件系统–VFS简介
http://www.embeddedlinux.org.cn/emb-linux/file-system/201712/20-7907.html 导读 Linux中可以支持多种文件系统,而且支持各种 ...
- Codeforces Round #620 (Div. 2) A-F代码 (暂无记录题解)
A. Two Rabbits (手速题) #include<bits/stdc++.h> using namespace std; typedef long long ll; int ma ...
- jQuery 判断页面上下滚动
var t = 0, b = 0; $(window).scroll(function(){ t = $(this).scrollTop(); if(b < t){ console.log('向 ...
- apache poi下载流程
apache poi下载官网:http://poi.apache.org/apidocs/4.1/ apache poi下载流程:https://blog.csdn.net/qq_31065001/a ...
- [tensorflow] tf.gather使用方法
tf.gather:用一个一维的索引数组,将张量中对应索引的向量提取出来 import tensorflow as tf a = tf.Variable([[1,2,3,4,5], [6,7,8,9, ...
- 《NVM-Express-1_4-2019.06.10-Ratified》学习笔记(5.23)-- Format NVM command
5.23 Format NVM command - NVM Command Set Specific Format NVM命令用于低级格式化NVM媒介.这个命令被host主机使用,来变更LBA数据大小 ...
- Ubuntu使用ipython出现版本不兼容的问题
今天租了一个ECS,然后准备安装了jupyter,准备配置,输入ipython,出现如下报错 ImportError: IPython 7.10+ supports Python 3.6 and ab ...
- Django Form组件的扩展
Form组件扩展 1.用Form组件自带的正则扩展 通过Django内置的字段:Validators自定义验证规则 方式一: from django.forms import Form from dj ...
- navicat 连接报2059错误
原因 navicat不支持mysql新版本的加密规则,mysql8 之前的版本中加密规则是mysql_native_password, mysql8之后,加密规则是caching_sha2_passw ...
- java mail发送html格式的邮件
// 获取系统属性 Properties properties = System.getProperties(); // 设置邮件服务器 properties.setProperty("ma ...