C++编程思想 第2卷 第3章 深入理解字符串 创建并初始化C++字符串

创建和初始化字符串对象是一件简单的事情并且相当灵活

第一个string对象imBlank虽然被声明了,但并不包含初始值

第二个heyMom,被文字参数 “Where are my socks ?”初始化,这种形式
的初始化使用一个引用字符数组作为string构造函数的参数

新创建的string对象做以下几件事:

创建空string对象,且并不立即用字符数据对其初始化

将一个文字的引用字符数组作为参数传递给构造函数,以此来对一个
string对象进行初始化

用等号 = 来初始化一个string对象

用一个string对象初始化另一个string对象

//: C03:SmallString.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
#include <string>
using namespace std;

int main() {
  string imBlank;
  string heyMom("Where are my socks?");
  string standardReply = "Beamed into deep "
    "space on wide angle dispersion?";
  string useThisOneAgain(standardReply);
  getchar();
} ///:~

无输出

这些都是string对象初始化最简单的形式,可灵活地进行初始化,进行
更好地进行初始化,并对其进行更好的控制 可以
1 使用C语言的char型数组或C++ string类两者任一个的一部分
2 用opearator+来将不同的初始化数据源结合在一起
3 用string对象的成员函数substr()来创建一个子串

//: C03:SmallString2.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
#include <string>
#include <iostream>
using namespace std;

int main() {
  string s1("What is the sound of one clam napping?");
  string s2("Anything worth doing is worth overdoing.");
  string s3("I saw Elvis in a UFO");
  // Copy the first 8 chars:
  string s4(s1, 0, 8);
  cout << s4 << endl;
  // Copy 6 chars from the middle of the source:
  string s5(s2, 15, 6);
  cout << s5 << endl;
  // Copy from middle to end:
  string s6(s3, 6, 15);
  cout << s6 << endl;
  // Copy many different things:
  string quoteMe = s4 + "that" +
  // substr() copies 10 chars at element 20
  s1.substr(20, 10) + s5 +
  // substr() copies up to either 100 char
  // or eos starting at element 5
  "with" + s3.substr(5, 100) +
  // OK to copy a single char this way
  s1.substr(37, 1);
  cout << quoteMe << endl;
  getchar();
} ///:~

string类对象的成员函数substr()将开始位置作为其第1个参数,而
将待选字符的个数作为其第2个参数

扫描二维码关注公众号,回复: 2843664 查看本文章


C++允许不同的string类对象初始化技术在单个语句中的混合使用,这是
一种灵活方便的特征

输出
What is
doing
Elvis in a UFO
What is that one clam doing with Elvis in a UFO?

稍微精巧些的初始化方法利用string类的迭代器string::begin()和
string::end() 
string类的构造函数传递两个迭代器,构造函数从一个迭代器开始
直到另一个迭代器结束,将它们之间的数据拷贝到新的string对象中

//: C03:StringIterators.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
#include <string>
#include <iostream>
#include <cassert>
using namespace std;

int main() {
  string source("xxx");
  string s(source.begin(), source.end());
  assert(s == source);
} ///:~

无输出

迭代器并不局限于begin()和end(),可以对一个对象使用的迭代器的
运算包括增1、减1以及加上整数偏移量,这些运算符允许程序员从
源string对象中提取字符的子集

不可以使用单个的字符、ASCII码或其他整数值来初始化C++字符串。
但是可以单个字符的多个拷贝来初始化字符串

//: C03:UhOh.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
#include <string>
#include <cassert>
using namespace std;

int main() {
  // Error: no single char inits
  //! string nothingDoing1('a');
  // Error: no integer inits
  //! string nothingDoing2(0x37);
  // The following is legal:
  string okay(5, 'a');
  assert(okay == string("aaaaa"));
} ///:~

无输出

第1个参数表示放入字符串中的第2个参数的拷贝的个数
第2个参数只能是单个字符的char型数据,而不是char型数组

猜你喜欢

转载自blog.csdn.net/eyetired/article/details/81663771