C中,Reserve函数有何作用?如何实现这个函数

C中,Reserve函数有何作用?如何实现这个函数,第1张

设置字符串的能力有很尺段大的数量至少为一个指定的次数。

#include <string>

#include <iostream>

int main( )

{

using namespace std

string str1 ("Hello world")

cout <<"The original string str1 is: " <<str1 <<endl

basic_string <char>::size_type sizeStr1, sizerStr1

sizeStr1 = str1.size ( )

basic_string <char>::size_type capStr1, caprStr1

capStr1 = str1.capacity ( )

// Compare size &capacity of the original string

cout <<"The current size of original string str1 is: "

<<sizeStr1 <<"." <<陵租誉 endl

cout <<"The capacity of original string str1 is: "

<<capStr1 <<"." <<endl <<endl

// Compare size &capacity of the string

// with added capacity

str1.reserve ( 40 )

sizerStr1 = str1.size ( )

caprStr1 = str1.capacity ( )

cout <<"The string str1with augmented capacity is: "

<<str1 <<endl

cout <<"The current size of string str1 is: "

<<sizerStr1 <<"." <<endl

cout <<"The new capacity of string str1 is: "

<<caprStr1 <<"." <<endl <<endl

// Compare size &capacity of the string

// with downsized capacity

str1.reserve ( )

basic_string <char>型含::size_type sizedStr1

basic_string <char>::size_type capdStr1

sizedStr1 = str1.size ( )

capdStr1 = str1.capacity ( )

cout <<"The string str1 with downsized capacity is: "

<<str1 <<endl

cout <<"The current size of string str1 is: "

<<sizedStr1 <<"." <<endl

cout <<"The reduced capacity of string str1 is: "

<<capdStr1 <<"." <<endl <<endl

}

#include <iostream>

#include <string>

using namespace std

string reserve(string arg)

{

    string result = ""

    for 液枝(int i = arg.length() - 1 i >吵盯= 升埋和0 --i)

    {

        result += arg[i]

    }

    return result

}

int main()

{

    cout<<reserve("abcd")<<endl

    return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/8155381.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存