string c++详解 7 copy data empty end

copy
size_t copy ( char* s, size_t n, size_t pos = 0) const;
Copy sequence of characters from string

Copies a sequence of characters from the string content to the array pointed bys. This sequence of characters is made of the characters in the string that start at character positionpos and spann characters from there.

The function does not append a null character after the content copied. To retrieve a temporary c-string value from a string object, a specific member function exists:
c_str.

将原字符串内容从Pos位置开始的n个字符复制能拷贝给字符数组s。

举例:

string str1 = "Hello World";

char ch1[20] = "";

str1.copy(ch1, 5, 6);

cout<<ch1<<endl;

//打印结果为 World

 

const char* data() const;
Get string data
 Returns a pointer to an array of characters with the same content as the string.
 
Notice that no terminating null character is appended (see member c_str for such a functionality).
 
The returned array points to an internal location which should not be modified directly in the program. Its contents are guaranteed to remain unchanged only until the next call to a non-constant member function of the string object.
 

用法同c_str()方法

 

举例:

string str4 = "Test string";

const char * cstr1 = str4.c_str();

const char * cstr2 = str4.data();

if((str4.compare(cstr1)==0)&&(str4.compare(cstr2)==0))

cout<<"both strings have the same content."<<endl;

//打印结果为 both strings have the same content.

 

bool empty ( ) const;
Test if string is empty

Returns whether the string is empty, i.e. whether itssize is0.

This function does not modify the content of the string in any way. To clear the content of the string, memberclear can be used.

如果字符串为空,则返回true,否则返回false

 

举例:

string str4 = "Test string";

str4.clear();

if(str4.empty())

cout<<"empty"<<endl;

//打印结果为empty

 

iterator end();const_iterator end() const;

Return iterator to end

Returns an iterator referring to the next element after the last character in the string.
返回字符串的结尾地址。类似begin()方法返回的是字符串的首地址。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值