求c++大神给说下string中的append()函数的用法
printf("Afterstrset():%s",string);append函数是向string 的后面追加字符或字符串。
c++stringfind函数 cstringfind函数
c++stringfind函数 cstringfind函数
c++stringfind函数 cstringfind函数
(1)向string 的后面加C-string
basic_string& append( const value_type _Ptr );
basic_string& append( const value_type _Ptr, size_type _Count );
basic_string& append( const basic_string& _Str );
basic_string& append( const basic_string& _Str, size_type _Off,
template
basic_string& append( size_type _Count, value_type _Ch );
参考:
要想使用标准C++中string类,必须要包含
#include
注意是
using
std::string;
using
或using
namespace
std;
下面你就可以使用string/wstring了,它们两分别对应着char和wchar_t。
string和wstring的用法是一样的,以下只用string作介绍:
string类的构造函数:
string(const
find_first_of(constchar
s);
//用c字符串s初始化
string(int
n,char
c);
//用n个字符c初始化
此外,string类还支持默认构造函数和构造函数,如string
s1;string
s2="hello";都是正确的写法。当构造的string太长而无法表达时会抛出length_error异常
;string类的字符作:
const
char
&operator[](int
n)const;
const
char
&at(int
n)const;
char
&operator[](int
n);
char
&at(int
n);
operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。
const
char
data()const;//返回一个非null终止的c字符数组
const
char
c_str()const;//返回一个以null终止的c字符串
copy(char
s,
n,
=0)
const;//把当前串中以开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目
string的特性描述:
capacity()const;
//返回当前容量(即string中不必增加内存即可存放的元素个数)
max_size()const;
//返回string对象中可存放的字符串的长度
size()const;
//返回当前字符串的大小
length()const;
//返回当前字符串的长度
bool
empty()const;
//当前字符串是否为空
void
resize(int
len,char
c);//把字符串当前大小置为len,并用字符c填充不足的部分
string类的输入输出作:
函数getline(istream
&in,string
&s);用于从输入流in中读取字符串到s中,以换行符'\n'分开。
string的赋值:
&operator=(const
&s);//把字符串s赋给当前字符串
&assign(const
char
s);//用c类型字符串s赋值
&assign(const
char
s,int
n);//用c字符串s开始的n个字符赋值
&assign(const
&s);//把字符串s赋给当前字符串
&assign(int
n,char
c);//用n个字符c赋值给当前字符串
&assign(const
start,int
n);//把字符串s中从start开始的n个字符赋给当前字符串
&assign(const_iterator
first,const_itertor
last);//把first和last迭代器之间的部分赋给字符串
string的连接:
&operator+=(const
&append(const
char
s);
//把c类型字符串s连接到当前字符串结尾
&append(const
char
s,int
n);//把c类型字符串s的前n个字符连接到当前字符串结尾
&append(const
&s);
//同operator+=()
&append(const
,int
n);//把字符串s中从开始的n个字符连接到当前字符串的结尾
&append(int
n,char
c);
//在当前字符串结尾添加n个字符c
&append(const_iterator
first,const_iterator
last);//把迭代器first和last之间的部分连接到当前字符串的结尾
string的比较:
bool
operator==(const
&s1,const
&s2)const;//比较两个字符串是否相等
运算符">","<",">=","<=","!="均被重载用于字符串的比较;
compare(const
&s)
const;//比较当前字符串和s的大小
compare(int
n,const
&s)const;//比较当前字符串从开始的n个字符组成的字符串与s的大小
compare(int
n,const
2,int
n2)const;//比较当前字符串从开始的n个字符组成的字符串与s中
//2开始的n2个字符组成的字符串的大小
compare(const
char
s)
compare(int
n,const
char
s)
compare(int
n,const
char
s,
2)
compare函数在>时返回1,<时返回-1,==时返回0
string的子串:
substr(int
=0,int
n=
n)
const;//返回开始的n个字符组成的字符串
string的交换:
void
swap(string
&s2);
//交换当前字符串与s2的值
string类的查找函数:
find(char
c,
=0)
const;//从开始查找字符c在当前字符串的位置
find(const
char
s,
=0)
const;//从开始查找字符串s在当前串中的位置
find(const
char
s,
n)
const;//从开始查找字符串s中前n个字符在当前串中的位置
find(const
&s,
=0)
const;//从开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::n的值
c,
=n)
const;//从开始从后向前查找字符c在当前串中的位置
rfind(const
char
s,
=n)
rfind(const
char
s,
n=
n)
rfind(const
=n)
//从开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::n的值
find_first_of(char
c,
=0)
const;//从开始查找字符c次出现的位置
char
s,
=0)
char
s,
n)
=0)
//从开始查找当前串中个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::n
find_first_not_of(char
c,
=0)
find_first_not_of(const
char
s,
=0)
find_first_not_of(const
char
s,
,int
n)
find_first_not_of(const
=0)
//从当前串中查找个不在串s中的字符出现的位置,失败返回string::n
find_last_of(char
c,
=n)
find_last_of(const
char
s,
=n)
find_last_of(const
char
s,
n=
n)
find_last_of(const
=n)
find_last_not_of(char
c,
=n)
find_last_not_of(const
char
s,
=n)
find_last_not_of(const
char
s,
n)
find_last_not_of(const
=n)
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找
C语言,问一个函数,检索并且返回某字符在某字符串中次出现的位置。是什么函数,返回值有哪些?急
const;如下: 函数名: strstr 功能: 在字符串中查找指定字符串的次出现 用法: char strstr(char str1, char str2); strstr原型:extern char strstr(char haystack, char needle); 头文件:#include
C++中如何提取string数组中的数字
int#include
#include
#include
using namespace std;
int main()
{string a[2]={"Y:3","N:4"};
int idx = a[0].find(':') + 1;
int t = atoi(a[0].substr(idx).c_str());
int k = atoi(a[1].substr(idx).c_str());
printf("%d, %d
", t, k);
}
C++11有stoi,stod,stof,等等函数
C++:怎样在string类中完成两个字符串的连接,?
std::wstring;当进行string对象和字符串字面值进行连接时,+作符的左右至少出现一个是string类型的。
string s1 = "hello";
string s2 = "world";
string s3 = s1 + ",";//ok
string s4 = "hello" + ",";//error
string s5 = s1 + "," + "world";ok
string s6 = "hello" + "," + s2;error
s5没错因为前面一个+相当于把前面两个先做temp = s1+",";的作,temp是字符串型,然后将temp与后面的进行相加;
s6错误的原因参照s4、s5就知道了。
string中的字符串拷贝:
String::String(const String &s):
_cstr(s._cstr),
_used(s._used),
_你可以这样写,void replace() /替换单词/length(s._length),
_capacity(s._capacity)
{++_used;
}
字符串的连接可以直接用加法
字符串的直接用copy函数
不明白可以继续讨论密我和我
祝你好运
使用strcat(a,b),就是把b连接到a的后面
字符串连接就是str3=str1+str2;
就是str1=str2;
c++中set函数和find函数的头文件是什么?
printf("%d#include
#include
include
include
c++中find_first_not_of函数的使用
find_first_of()函数:
查找在字符串中个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回string::n
查找在字符串中个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::n,
查找在字符串中个与ch匹配的字符,string返回它的位置。搜索从index开始。
函数find_first_not_of()功能如下: 1.返回在字符串中首次出现的★ Linux C语言字符与字符串处理不匹配str任何字符的首字符索引, 从index开始搜索, 如果全部匹配则返回string::n。 2.从index开始起搜索当前字符串, 查找其中与str前num个字符中的任意一个都不匹配的序列, 返回满足条件的个字符索引, 否则返回string::n。 3.返回在当前字符串中个不匹配ch字符的索引, 从index开始搜索, 没用收获则返回string::n。
rfind的用法是什么?
if (k==0) {n=POS[k]-80;} else {n=POS[k]-POS[k-1]-80;};find + 名词 ( /sth) 发现 【例】find a car。
find + + sth 为某人找到某物 【例句】 I wonder if you could find me a deck of cards? find + + doing sth /find + sth + Ved 发现。处于某种状态;遇见;碰见。
【例句1】They found her walking alone and depressed on the beach。
【例句2】She returned to her east London home to find her back door forced open。 find + sth + 介词 Thrushes are a protected specieints so you will not find them on any menu。
find + 宾语从句 【例句】The two biologists found, to their surprise, that both groups of birds survived equally well。这两位生物学家惊奇地发现,两组鸟都很好地存活了下来。 find it + adj. + to do sth 【例句】At my age I would find it hard to get another job。
到了我这个年纪自己就会发觉很难再找到一份工作了。 find + n. + in doing sth 【例句】How could anyone find pleasure in ting and killing this beautiful creature?怎么会有人以猎杀这种美丽的动物为乐呢?
另外,find 还可以作为名词,为发现的意思。【例句】His discovery was hailed as the botanical find of the century。他的发现被誉为本世纪植物学的最重大发现。
string中的find函数与rfind函数定义如下:
int find(char c, int = 0) const;//从开始查找字符c在当前字符串的位置。
int find(const char s, int = 0) const;//从开始查找字符串s在当前串中的位置。
int find(const char s, int , int n) const;//从开始查找字符串s中前n个字符在当前串中的位置。
int find(const string &s, int = 0) const;//从开始查找字符串s在当前串中的位置。
//查找成功时返回所在位置,失败返回string::n的值 。
int rfind(char c, int = n) const;//从开始从后向前查找字符c在当前串中的位置。
int rfind(const char s, int = n) const。
int rfind(const char s, int , int n = n) const。
int rfind(const string &s,int = n) const。
在实际的程序实现中,rfind的查找截止值并不是,而是+strlen(c)-1。
c语言中字符串查找函数是什么,调用格式是怎样的?
返回该字符串的首地址,用char表示,如果不存在,则返回空指针NULL
函数名: strsts4错误的原因是试图直接将两个字符串的字面值进行相加;r
功 能: 在串中查找指定字符串的次出现
用 法: char strstr(char str1, char str2);
程序例:
#include
#include
int main(void)
{char str1 = "Borland International", str2 = "nation", ptr;
ptr = strstr(str1, str2);
printf("The substring is: %s\n", ptr);
returnfor (i=0;i } char strchr(cs,c) return pointer to first occurrence of c in cs or NULL if not present. #include #include using namespace std; //使用find_first_of int main() {string s = "ab2c3d7R4E6"; string num = ("0123456789{find=1;break;}"); string letter = ("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"); string::size_type = 0; while(( = s.find_first_of(num, )) != string::n) {cout<<"found number at index: "<<<<" element is "< ++; }参数说明: = 0; while(( = s.find_first_of(letter, )) != string::n) {cout<<"found letter at index: "<<<<" element is "< ++; }return 0; }希望有帮助!! ?哪个find ???? 如果是 STL的. 一般是 map 和Hash_map iter =obj.find(key); if (iter !=&s);//把字符串s连接到当前字符串的结尾 obj.end()) {// find 有内容 do soming }STL 用迭代器判断 ,没印象用 -1 .返回值判断... #include #include #include #include using namespace std; int Count=0; / 函数名:findNum 作者:anglecloudy 描述:如果存在则返回字符串所在的位置,否则返回0,暂不支持文本中存在多个相同的串 先用test.txt文本测试,所有的文本作都是一样的,不管你怎么命名 / int findNum(char str) {FILE p; if((p=fopen("test.txt","rb"))==NULL) {printf("\n打开文件失败\n"); return 0; }char buffer[0x1000]; //保存文件 memset(buffer,0,0x1000); //初始化缓存 size_t fileLen=fread(buffer,sizeof(char),0x1000,p); //得到文件内容, int readLen=strlen(str); int IsFind=0; for(int i=0;i {if(strncmp(buffer+i,str,readLen)==0) {IsFind=i; }} fclose(p); return IsFind; }int main(void) {char str1="1234567"; int t1=0,t2=0; if((t1=findNum(str1))==0) {printf("没有找到字符串%s\n请按任意键退出\n",str1); return -1; }else {printf("字符串%s的位置在%d\n",str1,t1); }return 0; } 版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, b19126499425@163.com 举报,一经查实,本站将立刻删除。c++中find_first_not_of函数的使用
C++中find函数的返回值可以和-1或0比较吗????
补充:重载就是对已有的作符进行重新定义,以适应不同的数据类型,比如string s1 = string s2;int i1 = int i2;如果要用自定义的类型type_self,使得type_self tp1 = type_self tp2;一般的C++标准库里应该都有集成了,只有用到自己定义的数据类型就得重新写函数定义了。C语言中字符串的查找与替换
rfind(char