string常用成员函数
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常用成员函数的更多相关文章
- QString:常用成员函数总结
QString是Qt中使用频率最高的几种数据类型之一,主要在于其提供了大量功能强大的成员函数,这里重点介绍一些常用的成员函数: 一.字符串处理相关 1.1 split() (拆分字符串) split( ...
- String 的成员函数
本篇是把一些string的成员函数的用法记录下来 size()函数和lenth()函数 s.size()或者s.lenth() 它们都会返回长度,是总长度而不是下标长度 find函数 s.find(s ...
- string字符串成员函数
string字符串成员函数 string str1="aaa"; char c='c'; str1.assign("ABCAAAAAAABBBBB");//替换 ...
- <string>头文件常用成员函数
之前说过 string和vector一样,也是一种顺序容器,并且它也自带成员函数,用法和vector的成员函数差不多,不过它只能用来存放字符,也就是字符串. 在c++中,<string>基 ...
- C++中string的成员函数
string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...
- string简单成员函数的实现
原文:https://blog.csdn.net/zcyzsy/article/details/52146124 #include<iostream> using namespace st ...
- C#入门篇6-2:字符串操作 string常用的函数
//String 字符串的常见操作 public static void Fun1() { string MyStr = " Hello World! "; //length长度属 ...
- C/C++ 类成员函数指针 类成员数据指针
普通函数指针: "return_type (*ptr_name)(para_types) " 类成员函数指针: "return_type (class_name::*p ...
- String 类的实现(5)String常用函数
2 #include<iostream> 3 #include<stdio.h> 4 #include<assert.h> 5 #include <iom ...
随机推荐
- js动画函数
var tween = { linear: function (t, b, c, d) { return c * t / d + b; }, easeIn: function (t, b, c, d) ...
- 第十八次CSP认证游记 | 2019.12.15
CSP认证的考试是Haogod介绍的,取得一定成绩之后能有机会参加CCSP的分赛区和全国决赛.这次来参加认证要感谢老师的奔走为我们申请学校的报销,虽然最终因为这不是比赛所以报名费和差旅费下不来,但是老 ...
- 树莓派中安装ubuntu及相关设置
一.下载并烧录系统 首先准备好我们要烧录的ubuntu_meta系统,可以在树莓派官网中下载https://www.raspberrypi.org/downloads/ 这里我们选择 Raspberr ...
- C#之Quartz任务调度的使用(2.2.3.400)
这里使用的Quartz版本号为2.2.3.400,.net 框架为4.0. 目的实现一个小案例,每隔一秒钟打印一条记录.后面会附上源码,以供参考. 建立一个 控制台程序. 代码: class Pro ...
- 机器学习作业(一)线性回归——Python(numpy)实现
题目太长啦!文档下载[传送门] 第1题 简述:设计一个5*5的单位矩阵. import numpy as np A = np.eye(5) print(A) 运行结果: 第2题 简述:实现单变量线性回 ...
- [USACO10MAR] 伟大的奶牛聚集 - 树形dp
每个点有重数,求到所有点距离最小的点 就是魔改的重心了 #include <bits/stdc++.h> using namespace std; #define int long lon ...
- HTML-表格-基础表格
主要内容: HTML表格 基本语法和结构: 案例: border用在table标签里面,表示边框的. th标签是加粗,width是宽度,表格宽度用在table里面.: caption用在table ...
- Mapper-元素和属性
Mapper.xml文件内部的元素和属性 parameterType(输入类型) § 传递简单类型 § 使用#{}占位符,或者${}进行sql拼接, #{}括号中的值可以任意, ${}括号 ...
- linux安装Zookeeper及redis集群
亲测有效: 1.linux下安装zookeeper:https://www.cnblogs.com/zuolun2017/p/8186254.html 2.linux下安装redis集群:https: ...
- 一看就会一做就废系列:说说 RECOVER DATABASE(上)
这里是:一看就会,一做就废系列 数据库演示版本为 19.3 (12.2.0.3) 该系列涉及恢复过程中使用的 个语句: 1. recover database 2. recover database ...