c++ string详解 assign

assign方法可以理解为先将原字符串清空,然后赋予新的值作替换。

返回类型为 string类型的引用。其常用的重载也有下列几种:

a. string& assign ( const string& str );

将str替换原字串的内容

举例:

string testassign = "Hello World";

testassign.assign("Go home");

cout<<testassign<<endl;

//打印结果为 go home

b. string& assign ( const string& str, size_t pos, size_t n );

将str的内容从位置pos起的n个字符作为原字串的新内容赋给原字串

string testassign = "Hello World";

testassign.assign("Come on!", 5, 2);

cout<<testassign<<endl;

//打印结果为 on

c. string& assign ( const char* s, size_t n );

将字符数组或者字符串的首n个字符替换原字符串内容

举例:

string testassign = "Hello World";

testassign.assign("go back to China", 7);

cout<<testassign<<endl;

//打印结果为go back

d. string& assign ( const char* s );

将字符串或者字符数组作为新内容替换原字串

举例:

string testassign = "Hello World";

char ch[20] = "go back to shanghai";

testassign.assign(ch);

cout<<testassign<<endl;

//打印结果为 go back to shanghai

e. string& assign ( size_t n, char c );

将原字串替换为n个字符c

举例:

string testassign = "Hello World";

char ch = ‘?‘;

testassign.assign(5, ch);

cout<<testassign<<endl;

//打印结果为?????

f. template <class InputIterator>   string& assign ( InputIterator first, InputIterator last );

需要include <iterator>

举例:

string testassign = "Hello World";

testassign.assign(istream_iterator<char>(cin), istream_iterator<char>());

//输入abcde

cout<<testassign<<endl;

//打印结果为 abcde

---------------------------------------------------------------------------------------

string& assign ( const string& str );
string& assign ( const string& str, size_t pos, size_t n );
string& assign ( const char* s, size_t n );
string& assign ( const char* s );
string& assign ( size_t n, char c );
template <class InputIterator>
   string& assign ( InputIterator first, InputIterator last );

Assign content to string

Assigns new content to the string replacing its current content.

The arguments passed to the function determine the new content:

string& assign ( const string& str );
Sets a copy of str as the new content.
string& assign ( const string& str, size_t pos, size_t n );
Sets a copy of a substring of str as the new content. The substring is the portion of str that begins at the character position pos and takes up to n characters (it takes less than n if the end of str is reached before).
string& assign ( const char * s, size_t n );
Sets as the new content a copy of the string formed by the first n characters of the array pointed by s.
string& assign ( const char * s );
Sets a copy of the string formed by the null-terminated character sequence (C string) pointed by s as the new content. The length of the character sequence is determined by the first ocurrence of a null character (as determined by traits.length(s)).
string& assign ( size_t n, char c );
Sets a string formed by a repetition of character cn times, as the new content.
template<class InputIterator> string& assign (InputIterator first, InputIterator last);
If InputIterator is an integral type, behaves as the previous member function version, effectively setting as the new content a string formed by the repetition first times of the character equivalent to last.
In any other case, the content is set to the values of the elements that go from element referred to by iterator first to the element right before the one referred to by iterator last.

Parameters

str
Another object of class string whose content is entirely or partially copied as the new content for the string.
pos
Starting position of the substring of the string object str that forms the new content. Notice that the first position has a value of 0, not 1.
If the position passed is past the end of str, an out_of_range exception is thrown.
n
Number of characters to use for the content (i.e., its length).
s
Array with a sequence of characters.
In the third member function version, the length is determined by parameter n, even including null characters in the content.
By contrast, in the fourth member version, s is a null-terminated character, and therefore its length is determined only by the first occurrence of a null character.
c
Character value to be repeated n times to form the new content.
start
If along with last, both are integers, it is equivalent to parameter n, otherwise it is an iterator referring to the beginning of a sequence of characters.
last
If along with start, both are integers, it is equivalent to parameter c, otherwise it is an iterator referring to the past-the-end element of a sequence of characters.

Return Value

*this

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// string::assign
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str;
  string base="The quick brown fox jumps over a lazy dog.";

  // used in the same order as described above:

  str.assign(base);
  cout << str << endl;

  str.assign(base,10,9);
  cout << str << endl;         // "brown fox"

  str.assign("pangrams are cool",7);
  cout << str << endl;         // "pangram"

  str.assign("c-string");
  cout << str << endl;         // "c-string"

  str.assign(10,‘*‘);
  cout << str << endl;         // "**********"

  str.assign<int>(10,0x2D);
  cout << str << endl;         // "----------"

  str.assign(base.begin()+16,base.end()-12);
  cout << str << endl;         // "fox jumps over"

  return 0;
}
时间: 2024-08-03 08:36:44

c++ string详解 assign的相关文章

C++ string详解【转】

1.声明一个C++字符串 2.字符串操作函数 2.1 C++字符串和C字符串的转换 2.2 大小和容量函数 2.3元素存取 2.4比较函数 2.5 更改内容 插入(insert).删除(erase).替换(replace).增加字符 2.6提取子串和字符串连接 2.7输入输出操作 2.8搜索与查找 C++ string详解 Nicolai M.Josuttis 译者: 侯捷/孟岩 C++之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否

HUD5282 Senior&#39;s String 详解(使用DP解决组合数学)

题意:假设两个字符串的最长公共子序列长度为L,求第一个字符串中有多少个长度为L的子序列是第二个字符串的子序列.显然找出一个字符串的所有长度为L的子序列是组合数学问题,如果枚举所有子串的时间复杂度是n! 级的.这里就需要用动态规划来解决.首先用dp[i][j]和num[i][j]分别记录x的前I个字母和y的前j 个字母的最长公共子序列的长度和个数.先求出dp, 然后求num:.求num[i][j]分为两种情况,子序列不选x[i]和选x[i]: 1. 不选x[i]: 如果dp[i][j] == dp

[读书笔记]C#学习笔记八:StringBuilder与String详解及参数传递问题剖析

前言 上次在公司开会时有同事分享windebug的知识, 拿的是string字符串Concat拼接 然后用while(true){}死循环的Demo来讲解.其中有提及string操作大量字符串效率低下的问题, 刚好自己之前也看过类似的问题, 于是便拿出来记录一下.本文内容: 参数传递问题剖析, string与stringbuilder详解 1,参数传递问题剖析 对于C#中的参数传递,根据参数的类型可以分为四类: 值类型参数的按值传递 引用类型参数的按值传递 值类型参数的按引用传递 引用类型参数的

【Java的String详解】

Java的String类在开发时经常都会被使用到,由此可见String的重要性.经过这次认真仔细的学习了java的String类.发现了自己以前开发中对String使用的不足,特写此博客来记录自己对String的学习总结. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. String实际上是使用字符数组来存储的数据的.从源码可以清晰看到定义的字符数组. Stri

C++中的string详解

标准库类型string表示可变长的字符序列,为了在程序中使用string类型,我们必须包含头文件: #include <string>  声明一个字符串 声明一个字符串有很多种方式,具体如下: 1 string s;//调用默认构造函数,s为一个空字符串 2 string s(str);//等价于string s = str;调用拷贝构造函数,s是str的备份 3 string s(str,strindex);//将字符串str内始于strindex位置的部分当作s的初始值 4 eg.stri

C++ 中string 详解 转载自 博客园

转载自 http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 标准C++中的string类的用法总结 相信使用过MFC编程的朋友对CString这个类的印象应该非常深刻吧?的 确,MFC中的CString类使用起来真的非常的方便好用.但是如果离开了MFC框架,还有没有这样使用起来非常方便的类呢?答案是肯定的.也许有人会 说,即使不用MFC框架,也可以想办法使用MFC中的API,具体的操作方法在本文最后给出操作方法.其实,可

【c++】string详解

我能不用char*就不用,而使用C++标准程序库中的string类.string不必担心内存.字符长度等等的问题,并且string作为一个类,它的操作函数能够基本满足我的需要.string使用起来非常简单,我们用=赋值,用==比较是否相同,用+合并字符等等. 使用之前需要包含头文件 #include<string> 1.声明字符,调用构造函数初始化字符串 a.将strA赋值为空字符. string strA; b.复制字符串赋初值,将“B"赋值给strB,将strB赋值给strA.

JAVA: String详解

String 类用来存储字符串 ,是不可变的. 1. 基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean 他们之间的比较,应用双等号(==),比较的是他们的值. 2. 复合数据类型(类) 当他们用(==)进行比较的时候,比较的是他们在内存中的存放地址,所以,除非是同一个new出来的对象,他们的比较后的结果为true,否则比较后结果为false.用 str.equals(str2) 方法来比较字符串的值是否相等. 3. len

C++ string详解

转载至http://www.renfei.org/blog/introduction-to-cpp-string.html 运算符重载 + 和 +=:连接字符串 =:字符串赋值 >.>=.< 和 <=:字符串比较(例如a < b, aa < ab) ==.!=:比较字符串 <<.>>:输出.输入字符串 注意:使用重载的运算符 + 时,必须保证前两个操作数至少有一个为 string 类型.例如,下面的写法是不合法的: #include <io