string的 append()、substr()、size()的使用

 

string s = "";
cout<<s.size()<<endl;
输出 0

 

/*
 * string s = "";
 * cout<<s.size()<<endl;
 * 输出 0
 * 
 * substr()的使用
 * append()函数的使用
 * 
 * */
 
#include <iostream>
#include <string.h>
using namespace std;

void fuck(string str)
{
    if (str.size() == 0)
        return ;
    if(str.size() <= 8)
    {
        str.append(8-str.size(),'0');
        cout<<str<<endl;
        return;
    }
    cout<<str.substr(0,8)<<endl;
    fuck(str.substr(8));
}
int main()
{
        string str1,str2;
        while (getline(cin,str1))
        {
            getline(cin,str2);
            fuck(str1);
            fuck(str2);
        }
        return 0;
}

 

常用技巧:

一个整数怎么变成字符串??

取出整数的某一位  n ,那么怎么把它变成字符呢??  '0'+n  即可!! 然后append到字符串中。

1.substr

string substr (size_t pos = 0, size_t len = npos) const;
产生子串

返回一个新建的初始化为string对象的子串的拷贝string对象。
子串是,在字符位置pos开始,跨越len个字符(或直到字符串的结尾,以先到者为准)对象的部分。

参数

pos
第一个字符的位置被复制为子串。
如果这是等于字符串的长度,该函数返回一个空字符串。
如果这是大于字符串的长度,它会抛出out_of_range。
注意:第一个字符表示为值0(不是1)。
len
字符数在子包括(如果字符串是短,尽可能多的字符可以在需要使用)。
字符串::非营利值表示的所有字符,直到字符串的结尾。
// string::substr
#include <iostream>
#include <string>

int main ()
{
  std::string str="We think in generalities, but we live in details.";
                                           // (quoting Alfred N. Whitehead)

  std::string str2 = str.substr (3,5);     // "think"

  std::size_t pos = str.find("live");      // position of "live" in str

  std::string str3 = str.substr (pos);     // get from "live" to the end

  std::cout << str2 << ' ' << str3 << '\n';

  return 0;
}

Output:

think live in details

 

2.append

向string 的后面加字符或字符串。(比+=, push_back 更灵活)

 

(1)向string 的后面加C-string

basic_string& append( const value_type* _Ptr );

string s ( "Hello " ); // s=”Hello ”

const char *c = "Out There ";

s.append ( c ); // s=”Hello Out There”

 

(2)向string 的后面加C-string 的一部分

basic_string& append( const value_type* _Ptr, size_type _Count );

string s ( "Hello " ); // s=”Hello ”

const char *c = "Out There ";

s.append ( c , 3 ); // s=”Hello Out”

 

(3)向string 的后面加string(有两种方法)

basic_string& append( const basic_string& _Str );

string s1 ( "Hello " ), s2 ( "Wide " ), s3( "World " );

s1.append ( s2 ); // s1=”Hello Wide”

s1 += s3; // s1=”Hello Wide World”

 

(4)向string 的后面加string 的一部分 ---A

basic_string& append( const basic_string& _Str, size_type _Off,

size_type _Count );

string s1 ( "Hello " ), s2 ( "Wide World " );

s1.append ( s2 , 5 , 5 ); // s1=”Hello World”

 

(5)向string 的后面加string 的一部分 ---B

template<class InputIterator> basic_string& append(

InputIterator _First, InputIterator _Last );

string str1f ( "Hello " ), str2f ( "Wide World" );

str1f.append ( str2f.begin ( ) + 5 , str2f.end ( ) );

// s1=”Hello World”

 

(6)向string 的后面加一个或多个字符

basic_string& append( size_type _Count, value_type _Ch );

string str1e ( "Hello " );

str1e.append ( 1 , '0' ); // s1=”Hello0”

str1e.append ( 4 , '!' ); // s1=”Hello !!!!”

 

 

 

posted @ 2017-06-13 18:45  ren_zhg1992  阅读(307)  评论(0)    收藏  举报