C++ 分割字符串函数,并且返回vector
“Owenyu”通过精心收集,向本站投稿了8篇C++ 分割字符串函数,并且返回vector,以下是小编为大家整理后的C++ 分割字符串函数,并且返回vector,仅供参考,欢迎大家阅读。
篇1:C++ 分割字符串函数,并且返回vector
#include
#include
#include
using namespace std;
vector
{
vector
int i=0;
int j=0;
//去除字符串左边所有空格
while(str[i]==' ')
{
i++;
}
j=i;
//遍历字符串
while(i
{
if(str[j]==' ' || str[j]==' ')
{
result.push_back(str.substr(i,j-i));
j++;
i=j;
}
else
{
j++;
}
}
return result;
}
void out(vector
{
for(int i=0;i
{
cout<
}
}
int main
{
string line=“hello welcome to see us!”;
vector
out(result);
return 0;
}
篇2:SQL中自己创建函数,分割字符串
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[getEPnum]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[getEPnum]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[getstrcount]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[getstrcount]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[getstrofindex]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[getstrofindex]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
--- 这个函数直接调用了另外的两个函数,可以先阅读下面提到的两个函数
CREATE function getEPnum (@str varchar(8000))
returns varchar(8000)
as
begin
declare @str_return varchar(8000)
declare @i int
declare @temp_i int
declare @onlineornot int
declare @findepnumok int
-- 用来取得一个epnum,
-- 规则:首先从chatid中取,如果有在线得,则取得最前面得在线得返回
-- 如果全部不在线,则返回 ‘00000000’
select @findepnumok = 0
select @temp_i = 0
IF len(@str)<=0
begin
SELECT @str_return = '00000000'
end
else
begin
select @i = dbo.getstrcount(@str,',')
WHILE @temp_i<@i
BEGIN
select @onlineornot = online from wwchat_user where epnum=dbo.getstrofindex(@str,',',@temp_i)
IF (@onlineornot=1)
begin
select @str_return =dbo.getstrofindex(@str,',',@temp_i)
select @findepnumok = 1 --找到epnum后置为1
BREAK
end
ELSE
begin
select @temp_i = @temp_i + 1
select @findepnumok = 0 --找不到epnum后置为1
end
END
if @findepnumok = 0
begin
SELECT @str_return = '00000000'
end
end
return @str_return
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
--getstrcount 输入一个没有分割的字符串,以及分割符
--返回数组的个数
CREATE function getstrcount (@str varchar(8000),@splitstr varchar(100))
--returns varchar(8000)
returns int
as
begin
declare @int_return int
declare @start int
declare @next int
declare @location int
select @next = 0
select @location = 1
if len(@str)
select @int_return =0
if charindex(@splitstr,@str) = 0
select @int_return =0
while (@location0)
begin
select @start = @location + 1
select @location = charindex(@splitstr,@str,@start)
select @next = @next + 1
select @int_return = @next
end
return @int_return
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
-- getstrofindex 输入一个未分割的字符串,舒服分割符号,舒服要取得的字符位置
-- 返回 制定位置的字符串
CREATE function getstrofindex (@str varchar(8000),@splitstr varchar(4),@index int=0)
returns varchar(8000)
as
begin
declare @str_return varchar(8000)
declare @start int
declare @next int
declare @location int
select @start =1
select @next = 1 --如果习惯从0开始则select @next =0
select @location = charindex(@splitstr,@str,@start)
while (@location 0 and @index > @next )
begin
select @start = @location +1
select @location = charindex(@splitstr,@str,@start)
select @next =@next +1
end
if @location =0 select @location =len(@str)+1 --如果是因为没有逗号退出,则认为逗号在字符串后
select @str_return = substring(@str,@start,@location -@start) --@start肯定是逗号之后的位置或者就是初始值1
if (@index @next ) select @str_return = '' --如果二者不相等,则是因为逗号太少,或者@index小于@next的初始值1,
SQL中自己创建函数,分割字符串
,
return @str_return
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
篇3:c语言中字符串函数的使用
#include
#include
/*
char s1[]=“I am a student”;
char s2[20]=“teacher”;
char s3[]=“student”;
int result;
char s4[20],*p;
1.串的长度
int strlen(char *str):
printf(“%dn”,strlen(s1));//长度为14
printf(“%dn”,strlen(s2));//长度为7
2.复制
char *strcpy(char *str1,char *str2):
strcpy(s4,s2);//把s2复制给s4
printf(“%sn”,s4);//输出teacher
3.比较
int strcmp(char *str1,char *str2):
result=strcmp(s2,s3);
printf(“%dn”,result);//s2>s3
4.字符串定位
char *strchr(char *str,char ch);
p=strchr(s1,'s');//p指向在s1中字符's'的位置
printf(“%sn”,p);//输出student
5.子串查找
char *strstr(char *s1,char *s2);
p=strstr(s1,s3);//p指向在s1中字符's'的位置
printf(“%sn”,p);//输出student
6.连接
char * strcat(char *str1,char *str2):
strcat(s2,s3);
printf(“%sn”,s2);//输出teacherstudent
*/
void ReverseName(char *name,char *newName){
char *p;
p=strchr(name,' ');//字符定位
*p=' ';
printf(“%sn”,name);
printf(“%sn”,p);
strcpy(newName,p+1);//复制
printf(“--%sn”,newName);
strcat(newName,“,”);//连接
strcat(newName,name);//连接
*p=' ';
printf(“%sn”,name);
}
int main{
char name[]=“jie wang”,newName[30];
ReverseName(name,newName);
printf(“hello worldn”);
return 0;
}
篇4:(C语言)字符串比较函数,指针数组与数组指针
问题描述:
写一个函数,用于比较两个字符串的比较(string_compare).
程序分析:
(1)主要思想:传入两个字符串后,比较这两个字符串中的每个元素,如果第一次比较就不相等,就不要让它进入到下面的比较中,这样一来,将它返回一个相减的值(即:两数组中开始不相等的那两个元素相减,返回值(int类型),是ASCII码值相减)。进入比较的过程中时,相等就返回0;其他情况都返回那个相减的值。
(2)主要方式:定义指针数组,并对其初始化。然后照上面的思想,进行代码的实现。
代码如下:
/***指针数组(1)int *a[10] 是一个指针数组--->是一个数组(每个数组中的元素都是int*类型)(2)int (*a)[10] 是一个数组指针--->指向一个数组(十个int类型的数组) 注意:*,[],的优先级依次递增。下面使用了指针数组的例子,至于数组指针。。**/#include
篇5:Dephi 中优秀的字符串分割函数.net
DELPHI没有自己的字符串分割函数,所以只能 程序员 自己写了,网上搜了好多但是真正好用的没有几个, 下面这个是我在网上找到修改后了的,个人感觉算法不错,所以就贴了上来, functionSplitString(Source,Deli:string):TStringList;s td call; var EndOfCurr
DELPHI没有自己的字符串分割函数,所以只能程序员自己写了,网上搜了好多但是真正好用的没有几个,下面这个是我在网上找到修改后了的,个人感觉算法不错,所以就贴了上来。
function SplitString(Source, Deli: string ): TStringList;stdcall;
var
EndOfCurrentString: byte;
StringList:TStringList;
begin
StringList:=TStringList.Create;
while Pos(Deli, Source)>0 do
begin
EndOfCurrentString := Pos(Deli, Source);
StringList.add(Copy(Source, 1, EndOfCurrentString - 1));
Source := Copy(Source, EndOfCurrentString + length(Deli), length(Source) - EndOfCurrentString);
end;
Result := StringList;
StringList.Add(source);
end;
本文引用通告地址: blog.csdn.net/mahuzi/services/trackbacks/502563.aspx
原文转自:www.ltesting.net
篇6:C语言 字符串的内存拷贝处理函数
对于字符串来说,我们运用字符串 C语言 字符串的内存拷贝处理函数
这个是
void *memcpy(void *dest,const void *src,size_t count);
与strcpy不同的就是添加了第三个参数,确定操作的字节数,然后参数类型还有返回类型都是void*
,这表示他可以拷贝任意类型的数据。
然后我们看一下实现:
memcpy:
void *my_memcpy(void *str,const void *Dstr,int count) //从内存地址开始改变,并确定改变长度,所以用万能类型去接受{ char *pstr = (char *)str; char *pDstr = (char *)Dstr;assert((str!=NULL) && (Dstr != NULL)); if(str == Dstr) //位置相同情况下直接返回需要改变的 return (char *)Dstr; while(count-- > 0){*pstr++ = *pDstr++;} return str;}
然后会出现一个问题,如果我们拷贝的数据中Dstr的起始位置在STR操作之间,那么在改变str时会出现副作用,将导致我们的拷贝结果不正确,所以我们应该考虑到会覆盖的情况,
在函数库中有一个memmove函数。
memmove:
void *my_memmove(void *pst,const void *Dpst,int size){ void *p = pst; char *pstA = (char *)pst; char *pstB = (char *)Dpst;assert((pst != NULL) &&(Dpst != NULL)); if(pstB
就是遇到被拷贝的空间起始处在拷贝空间中,将会遇到拷贝内存覆盖的现象。在这种情况下我们将考虑从尾部进行拷贝。所以进行了判断。
篇7:SQL 中自己创建函数,分割字符串数据库教程
创建|函数|字符串
----------------------------------------------------------------
/**
* 版权: 石太祥 [ E.Alpha ] 所有 ;
*
* email: ealpha(AT)msn(DOT)com ;
* msn: ealpha(AT)msn(DOT)com ;
* QQ : 9690501
*
* 所有请注明本信息!
*/
----------------------------------------------------------------
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[getEPnum]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[getEPnum]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[getstrcount]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[getstrcount]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[getstrofindex]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[getstrofindex]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
--- 这个函数直接调用了另外的两个函数,可以先阅读下面提到的两个函数
CREATE function getEPnum (@str varchar(8000))
returns varchar(8000)
as
begin
declare @str_return varchar(8000)
declare @i int
declare @temp_i int
declare @onlineornot int
declare @findepnumok int
-- 用来取得一个epnum,
-- 规则:首先从chatid中取,如果有在线得,则取得最前面得在线得返回
-- 如果全部不在线,则返回 ‘00000000’
select @findepnumok = 0
select @temp_i = 0
IF len(@str)<=0
begin
SELECT @str_return = '00000000'
end
else
begin
select @i = dbo.getstrcount(@str,',')
WHILE @temp_i< @i
BEGIN
select @onlineornot = online from wwchat_user where epnum=dbo.getstrofindex(@str,',',@temp_i)
IF (@onlineornot=1)
begin
select @str_return =dbo.getstrofindex(@str,',',@temp_i)
select @findepnumok = 1 --找到epnum后置为1
BREAK
end
ELSE
begin
select @temp_i = @temp_i + 1
select @findepnumok = 0 --找不到epnum后置为1
end
END
if @findepnumok = 0
begin
SELECT @str_return = '00000000'
end
end
return @str_return
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
-- getstrcount 输入一个没有分割的字符串,以及分割符
--返回数组的个数
CREATE function getstrcount (@str varchar(8000),@splitstr varchar(100))
--returns varchar(8000)
returns int
as
begin
declare @int_return int
declare @start int
declare @next int
declare @location int
select @next = 0
select @location = 1
if len(@str)
select @int_return =0
if charindex(@splitstr,@str) = 0
select @int_return =0
while (@location0)
begin
select @start = @location + 1
select @location = charindex(@splitstr,@str,@start)
select @next = @next + 1
select @int_return = @next
end
return @int_return
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
-- getstrofindex 输入一个未分割的字符串,舒服分割符号,舒服要取得的字符位置
-- 返回 制定位置的字符串
CREATE function getstrofindex (@str varchar(8000),@splitstr varchar(4),@index int=0)
returns varchar(8000)
as
begin
declare @str_return varchar(8000)
declare @start int
declare @next int
declare @location int
select @start =1
select @next = 1 --如果习惯从0开始则select @next =0
select @location = charindex(@splitstr,@str,@start)
while (@location 0 and @index > @next )
begin
select @start = @location +1
select @location = charindex(@splitstr,@str,@start)
select @next =@next +1
end
if @location =0 select @location =len(@str)+1 --如果是因为没有逗号退出,则认为逗号在字符串后
select @str_return = substring(@str,@start,@location -@start) --@start肯定是逗号之后的位置或者就是初始值1
if (@index @next ) select @str_return = '' --如果二者不相等,则是因为逗号太少,或者@index小于@next的初始值1,
SQL 中自己创建函数,分割字符串数据库教程
,
return @str_return
end
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
篇8:C语言字符串拷贝strcpy函数的陷阱分析
在C语言中,我们都知道字符串是永恒的话题,字符串含有很多地雷,你稍不留心就会被砸到,比方说,字符串的结尾是' ',也是占一个字符空间的,那么如果我们在利用strcpy拷贝字符串的时候,应该多加1个字符空间,就是专门留给这个' '的,
如果我们不多加一个字符空间,会发生致命的错误,那么我们通过案例来说明。
-------------我是分割线------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# include
# include
# include
int main()
{
char str[] = “MengLiang”;
//此处分配空间没有考虑到' '
char* New_str = (char*)malloc(strlen(str));
strcpy(New_str, str);
printf(“The New_str = %sn”, New_str);
free(New_str);
New_str = NULL;
system(“pause”);
return 0;
}
-------------我是分割线------------------
我在注释中已经写了,那么这小段程序的结果呢?
-------------我是分割线------------------
正确的修改为:
-------------我是分割线------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# include
# include
# include
int main()
{
char str[] = “MengLiang”;
//此处的加1就是为' '来服务的
char* New_str = (char*)malloc(strlen(str)+1);
strcpy(New_str, str);
printf(“The New_str = %sn”, New_str);
free(New_str);
New_str = NULL;
system(“pause”);
return 0;
}
-------------我是分割线------------------
C语言的自由意味着我们要自制!
----------------------------------------------
本文出自 “享受科技带来的快乐” 博客,请务必保留此出处liam2199.blog.51cto.com/2879872/1420096
【C++ 分割字符串函数,并且返回vector】相关文章:






文档为doc格式