string常用成员函数

std::string::clear

Clear string

Erases the contents of the string, which becomes an empty string (with a length of 0 characters).

Parameters

none

Return value

none

Example

// string::clear
#include <iostream>
#include <string> int main ()
{
char c;
std::string str;
std::cout << "Please type some lines of text. Enter a dot (.) to finish:\n";
do {
c = std::cin.get();
str += c;
if (c=='\n')
{
std::cout << str;
str.clear();
}
} while (c!='.');
return 0;
}

This program repeats every line introduced by the user until a line contains a dot ('.'). Every newline character ('\n') triggers(触发) the repetition of the line and the clearing of the current string content.

const char *c_str();
c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同.
这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
c_str函数的返回值是const char *,不能直接赋值给char *

compare用于比较两个字符串是否相等。

用法:

str1.compare(str2);

str1=str2,输出0;

str1>str2,输出>0;

str1<str2,输出<0。

std::string::empty

Test if string is empty

Returns whether the string is empty (i.e. whether its length is 0).

This function does not modify the value of the string in any way. To clear the content of a string, see string::clear.

Parameters

none

Return Value

true if the string length is 0, false otherwise.

Example

// string::empty
#include <iostream>
#include <string> int main ()
{
std::string content;
std::string line;
std::cout << "Please introduce a text. Enter an empty line to finish:\n";
do {
getline(std::cin,line);
content += line + '\n';
} while (!line.empty());
std::cout << "The text you introduced was:\n" << content;
return 0;
}

This program reads the user input line by line and stores it into string content until an empty line is introduced.

std::string::length/size

Return length of string

Returns the length of the string, in terms of bytes.

This is the number of actual bytes that conform the contents of the string, which is not necessarily equal to its storage capacity(存储容量).

Note that string objects handle bytes without knowledge of the encoding that may eventually be used to encode the characters it contains. Therefore, the value returned may not correspond to the actual number of encoded characters in sequences of multi-byte or variable-length characters (such as UTF-8).

Both string::size and string::length are synonyms and return the exact same value.

Parameters

none

Return Value

The number of bytes in the string.

size_t is an unsigned integral type (the same as member type string::size_type).

Example

// string::length/size
#include <iostream>
#include <string> int main ()
{
std::string str ("Test string");
std::cout << "The size of str is " << str.length() << " bytes.\n";
//std::cout << "The size of str is " << str.size() << " bytes.\n";
return 0;
}

Output:

The size of str is 11 bytes

string常用成员函数的更多相关文章

  1. QString:常用成员函数总结

    QString是Qt中使用频率最高的几种数据类型之一,主要在于其提供了大量功能强大的成员函数,这里重点介绍一些常用的成员函数: 一.字符串处理相关 1.1 split() (拆分字符串) split( ...

  2. String 的成员函数

    本篇是把一些string的成员函数的用法记录下来 size()函数和lenth()函数 s.size()或者s.lenth() 它们都会返回长度,是总长度而不是下标长度 find函数 s.find(s ...

  3. string字符串成员函数

    string字符串成员函数 string str1="aaa"; char c='c'; str1.assign("ABCAAAAAAABBBBB");//替换 ...

  4. <string>头文件常用成员函数

    之前说过 string和vector一样,也是一种顺序容器,并且它也自带成员函数,用法和vector的成员函数差不多,不过它只能用来存放字符,也就是字符串. 在c++中,<string>基 ...

  5. C++中string的成员函数

    string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...

  6. string简单成员函数的实现

    原文:https://blog.csdn.net/zcyzsy/article/details/52146124 #include<iostream> using namespace st ...

  7. C#入门篇6-2:字符串操作 string常用的函数

    //String 字符串的常见操作 public static void Fun1() { string MyStr = " Hello World! "; //length长度属 ...

  8. C/C++ 类成员函数指针 类成员数据指针

    普通函数指针:  "return_type (*ptr_name)(para_types) " 类成员函数指针: "return_type (class_name::*p ...

  9. String 类的实现(5)String常用函数

      2 #include<iostream> 3 #include<stdio.h> 4 #include<assert.h> 5 #include <iom ...

随机推荐

  1. 针对wordpress CMS的信息收集

    如果发现一个站点用的是wordpress管理系统的话, 可以试试默认的后台地址:/wp-admin/ 访问后自动跳转置 后台登录界面 用户名收集 :/?author=1 依次访问/author=1 , ...

  2. base(根URL)

    指定用于一个文档中包含的所有相对 URL 的根 URL.一份中只能有一个 <base> 元素. 可以通过使用 document.baseURI 的 JS 脚本查询 属性 包含全局属性 hr ...

  3. 安装SQL Server2008出现Restart computer failed的解决办法

    1.打开注册表编辑器 2.找到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager双击文件夹 3.找到PendingF ...

  4. docker-储存持久化

    docker容器不适合存放数据,重要的数据要用外部卷存储,容器可以挂载真实机目录或者共享存储为卷 储存卷映射 docker run -itd -v 真实机目录:容器目录 镜像:标签 可以做一台nfs服 ...

  5. springBoot 2.1.5 pom 文件 unknown 错误

    插件不兼容 pom文件添加 <properties>    <maven-jar-plugin.version>2.6</maven-jar-plugin.version ...

  6. AtCoder arc078_d Mole and Abandoned Mine

    洛谷题目页面传送门 & AtCoder题目页面传送门 给定一个无向连通带权图\(G=(V,E),|V|=n,|E|=m\)(节点从\(0\)开始编号),要删掉一些边使得节点\(0\)到\(n- ...

  7. HTML表格显示的笔记

    有时需要显示的复杂表头 如图所示  <table id="" cellpadding="0" cellspacing="0" bord ...

  8. js和jq跳转到另一个页面或者在另一个窗口打开页面

    $("#pic").click(function(){ location.href='newpage.html'; }); 上面的相当于<a href="newpa ...

  9. 素问 - IC移仓换月

    摘自<小韭的学习圈> Q 股指期货的合约什么时候换月比较合适? 今天是1908股指期货的交割日,我是这么操作的:我在10:30分把IH1908以2827元卖出,然后马上以2805.8元买入 ...

  10. Android监听消息通知栏点击事件

    Android监听消息通知栏点击事件 使用BroadCastReceiver 1 新建一个NotificationClickReceiver 类,并且在清单文件中注册!! public class N ...