C++之std::string ------ append用法

语法1:附加字符串str的字符。如果结果大小超过最大字符数,它将抛出length_error。

string& string::append (const string& str)

str: is the string to be appended.
返回: *this

例子:

#include <iostream> 
#include <string> 
using namespace std; 
   
// Function to demonstrate append() 
void appendDemo(string str1, string str2) 
{ 
    // Appends str2 in str1 
    str1.append(str2); 
    cout << "Using append():"; 
    cout << str1 << endl; 
} 
   
// Driver code 
int main() 
{ 
    string str1("Hello World! "); 
    string str2("GeeksforGeeks"); 
   
    cout << "Original String:" << str1 << endl; 
    appendDemo(str1, str2); 
   
    return 0; 
}

输出
Original String:Hello World! 
Using append():Hello World! GeeksforGeeks

 语法2:最多附加字符串str的str_num字符,从索引str_idx开始。如果str_idx> str,则抛出out_of_range。 size()。如果结果大小超过最大字符数,它将抛出length_error。

string& string::append (const string& str, size_type str_idx, size_type str_num)

str: is the string to be appended
str_num: being number of characters
str_idx: is index number.
返回:*this.

 例子:

#include <iostream> 
#include <string> 
using namespace std; 
   
// Function to demonstrate append() 
void appendDemo(string str1, string str2) 
{ 
    // Appends 5 characters from 0th index of 
    // str2 to str1 
    str1.append(str2, 0, 5); 
    cout << "Using append():"; 
    cout << str1; 
} 
   
// Driver code 
int main() 
{ 
    string str1("GeeksforGeeks "); 
    string str2("Hello World! "); 
   
    cout << "Original String:" << str1 << endl; 
    appendDemo(str1, str2); 
   
    return 0; 
}
输出:

Original String:GeeksforGeeks 
Using append():GeeksforGeeks Hello

语法3:追加字符数组chars的chars_len字符。如果结果大小超过最大字符数,则抛出length_error。

string& string::append (const char* chars, size_type chars_len)

*chars is the pointer to character array to be appended.
chrs_len: is the number of characters from *chars to be appended.
Note that chars must have at least chars_len characters. 
返回: *this.

例子:

#include <iostream> 
#include <string> 
using namespace std; 
   
// Function to demonstrate append 
void appendDemo(string str) 
{ 
    // Appends 5 characters from "GeeksforGeeks" 
    // to str 
    str.append("GeeksforGeeks", 5); 
    cout << "Using append():"; 
    cout << str << endl; 
} 
   
// Driver code 
int main() 
{ 
    string str("World of "); 
   
    cout << "Original String:" << str << endl; 
    appendDemo(str); 
   
    return 0; 
}

输出:

Original String:World of 
Using append():World of Geeks

语法4:追加字符c的出现次数。如果结果大小超过最大字符数,则抛出length_error。

string& string::append (size_type num, char c)

num: is the number of occurrences
c:is the character which is to be appended repeatedly. 
返回: *this.

 例子:

#include <iostream> 
#include <string> 
using namespace std; 
    
// Function to demonstrate append 
void appendDemo(string str) 
{ 
    // Appends 10 occurrences of '$' 
    // to str 
    str.append(10, '$'); 
    cout << "After append():"; 
    cout << str; 
   
} 
           
// Driver code 
int main() 
{ 
    string str("#########"); 
   
    cout << "Original String:" << str << endl; 
    appendDemo(str); 
    
    return 0; 
}
输出:

Original String:#########
After append():#########$$$$$$$$$$

语法5:追加范围[beg,end)的所有字符。如果结果大小超过最大字符数,则抛出length_error

string& string::append (InputIterator beg, InputIterator end)

first, last:Input iterators to the initial and final positions 
in a sequence.
返回 *this.

例子:

#include <iostream> 
#include <string> 
using namespace std; 
   
// Function to demonstrate append 
void appendDemo(string str1, string str2) 
{ 
   
    // Appends all characters from 
    // str2.begin()+5, str2.end() to str1 
    str1.append(str2.begin() + 5, str2.end()); 
    cout << "Using append:"; 
    cout << str1; 
} 
// Driver code 
int main() 
{ 
    string str1("Hello World! "); 
    string str2("GeeksforGeeks"); 
   
    cout << "Original String:" << str1 << endl; 
    appendDemo(str1, str2); 
   
    return 0; 
}

输出:

Original String:Hello World! 
Using append:Hello World! forGeeks

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
所有的 C / C++ 函数 Constructors (cppstring) Constructors (cppvector) Operators (cppbitset) Operators (cppdeque) Operators (cppstack) Operators (cppstring) Operators (cppvector) abort (stdother) abs (stdmath) acos (stdmath) any (cppbitset) append (cppstring) asctime (stddate) asin (stdmath) assert (stdother) assign (cppdeque) assign (cpplist) assign (cppstring) assign (cppvector) at (cppdeque) at (cppstring) at (cppvector) atan (stdmath) atan2 (stdmath) atexit (stdother) atof (stdstring) atoi (stdstring) atol (stdstring) back (cppdeque) back (cpplist) back (cppqueue) back (cppvector) bad (cppio) begin (cppdeque) begin (cpplist) begin (cppmap) begin (cppmultimap) begin (cppmultiset) begin (cppset) begin (cppstring) begin (cppvector) bsearch (stdother) c_str (cppstring) calloc (stdmem) capacity (cppstring) capacity (cppvector) ceil (stdmath) clear (cppdeque) clear (cppio) clear (cpplist) clear (cppmap) clear (cppmultimap) clear (cppmultiset) clear (cppset) clear (cppvector) clearerr (stdio) clock (stddate) compare (cppstring) copy (cppstring) cos (stdmath) cosh (stdmath) count (cppbitset) count (cppmap) count (cppmultimap) count (cppmultiset) count (cppset) ctime (stddate) data (cppstring) #define (preproc) difftime (stddate) div (stdmath) empty (cppdeque) empty (cpplist) empty (cppmap) empty (cppmultimap) empty (cppmultiset) empty (cpppriorityqueue) empty (cppqueue) empty (cppset) empty (cppstack) empty (cppstring) empty (cppvector) end (cppdeque) end (cpplist) end (cppmap) end (cppmultimap) end (cppmultiset) end (cppset) end (cppstring) end (cppvector) eof (cppio) equal_range (cppmap) equal_range (cppmultimap) equal_range (cppmultiset) equal_range (cppset) erase (cppdeque) erase (cpplist) erase (cppmap) erase (cppmultimap) erase (cppmultiset) erase (cppset) erase (cppstring) erase (cppvector) #error (preproc) exit (stdother) exp (stdmath) fabs (stdmath) fail (cppio)
log4cplus是C++编写的开源的日志系统,功能非常全面,用到自己开发的工程中会比较专业的,:),本文介绍了log4cplus基本概念,以及如何安装,配置。 ### 简介 ### log4cplus是C++编写的开源的日志系统,前身是java编写的log4j系统.受Apache Software License保护。作者是Tad E. Smith。log4cplus具有线程安全、灵活、以及多粒度控制的特点,通过将信息划分优先级使其可以面向程序调试、运行、测试、和维护等全生命周 期; 你可以选择将信息输出到屏幕、文件、 NT event log、甚至是远程服务器;通过指定策略对日志进行定期备份等等。 ### 下载 ### 最新的log4cplus可以从以下网址下载 http://log4cplus.sourceforge.net本文使用的版本为:1.0.2 ### 安装 ### 1. linux下安装 tar xvzf log4cplus-x.x.x.tar.gz cd log4cplus-x.x.x ./configure --prefix=/where/to/install make make install 这里我采用缺省安装路径:/usr/local,下文如无特别说明,均以此路径为准。 2. windows下安装 不需要安装,有一个msvc6存放包括源代码和用例在内的开发工程(for VC6 only),使用之前请先编译 "log4cplus_dll class"工程生成dll,或者编译"log4cplus_static class"工程生成lib. ### 使用前的配置 ### 1. linux下的配置 确保你的Makefile中包含 /usr/local/lib/liblog4cplus.a(静态库)或 -llog4cplus(动态库)即可, 头文件在/usr/local/include/log4cplus目录下。对于动态库,要想正常使用,还得将库安装路径加入到 LD_LIBRARY_PATH 中,我一般是这样做的:以管理员身份登录,在/etc/ld.so.conf中加入安装路径,这里 是/usr/local/lib,然后执行ldconfig使设置生效即可。 2. windows下的配置 将"log4cplus_dll class"工程或"log4cplus_static class"工程的dsp 文件插入到你的工程中,或者直接 把两个工程编译生成的库以及头文件所在目录放到你的工程的搜索路径中,如果你使用静态库,请在你的工程中 "project/setting/C++"的preprocessor definitions中加入LOG4CPLUS_STATIC。 ### 构成要素介绍 ### 虽然功能强大,应该说log4cplus用起来还是比较复杂的,为了更好地使用它,先介绍一下它的基本要素。 Layouts :布局器,控制输出消息的格式. Appenders :挂接器,与布局器紧密配合,将特定格式的消息输出到所挂接的设备终端 (如屏幕,文件等等)。 Logger :记录器,保存并跟踪对象日志信息变更的实体,当你需要对一个对象进行 记录时,就需要生成一个logger。 Categories :分类器,层次化(hierarchy)的结构,用于对被记录信息的分类,层次中 每一个节点维护一个logger的所有信息。 Priorities :优先权,包括TRACE, DEBUG, INFO, WARNING, ERROR, FATAL。 本文介绍了log4cplus基本概念,以及如何安装,配置,下一篇将通过例子介绍如何使用log4cplus。 (二) 本文介绍了使用log4cplus有六个步骤,并提供了一些例子引导你了解log4cplus的基本使用。 ### 基本使用 ### 使用log4cplus有六个基本步骤: 1. 实例化一个appender对象 2. 实例化一个layout对象 3. 将layout对象绑定(attach)到appender对象 4. 实例化一个logger对象,调用静态函数:log4cplus::Logger::getInstance("logger_name") 5. 将appender对象绑定(attach)到logger对象,如省略此步骤,标准输出(屏幕)appender对象会绑定到logger 6. 设置logger的优先级,如省略此步骤,各种有限级的消息都将被记录 下面通过一些例子来了解log4cplus的基本使用。 〖例1〗 cpp 代码 /* 严格实现步骤1-6,appender输出到屏幕, 其中的布局格式和LogLevel后面会详细解释。*/ #include #include #include using namespace log4cplus; using namespace log4cplus::helpers; int main(){ /* step 1: Instantiate an appender object */ SharedObjectPtr _append (new ConsoleAppender()); _append->setName("append for test"); /* step 2: Instantiate a layout object */ std::string pattern = "%d{%m/%d/%y %H:%M:%S} - %m [%l]%n"; std::auto_ptr _layout(new PatternLayout(pattern)); /* step 3: Attach the layout object to the appender */ _append->setLayout( _layout ); /* step 4: Instantiate a logger object */ Logger _logger = Logger::getInstance("test"); /* step 5: Attach the appender object to the logger */ _logger.addAppender(_append); /* step 6: Set a priority for the logger */ _logger.setLogLevel(ALL_LOG_LEVEL); /* log activity */ LOG4CPLUS_DEBUG(_logger, "This is the FIRST log message...") sleep(1); LOG4CPLUS_WARN(_logger, "This is the SECOND log message...") return 0; } 输出结果: 10/14/04 09:06:24 - This is the FIRST log message... [main.cpp:31] 10/14/04 09:06:25 - This is the SECOND log message... [main.cpp:33] 〖例2〗 /* 简洁使用模式,appender输出到屏幕。 */ #include #include using namespace log4cplus; using namespace log4cplus::helpers; int main() { /* step 1: Instantiate an appender object */ SharedAppenderPtr _append(new ConsoleAppender()); _append->setName("append test"); /* step 4: Instantiate a logger object */ Logger _logger = Logger::getInstance("test"); /* step 5: Attach the appender object to the logger */ _logger.addAppender(_append); /* log activity */ LOG4CPLUS_DEBUG(_logger, "This is the FIRST log message...") sleep(1); LOG4CPLUS_WARN(_logger, "This is the SECOND log message...") return 0; } 输出结果: DEBUG - This is the FIRST log message... WARN - This is the SECOND log message... 〖例3〗 /* iostream模式,appender输出到屏幕。 */ #include #include #include /* 其实这个东东还是放到log4cplus头文件中比较合适些,个人意见:) */using namespace log4cplus; int main() { /* step 1: Instantiate an appender object */ SharedAppenderPtr _append(new ConsoleAppender()); _append->setName("append test"); /* step 4: Instantiate a logger object */ Logger _logger = Logger::getInstance("test"); /* step 5: Attach the appender object to the logger */ _logger.addAppender(_append); /* log activity */ LOG4CPLUS_TRACE(_logger, "This is" << " just a t" << "est." << std::endl) LOG4CPLUS_DEBUG(_logger, "This is a bool: " << true) LOG4CPLUS_INFO(_logger, "This is a char: " << 'x') LOG4CPLUS_WARN(_logger, "This is a int: " << 1000) LOG4CPLUS_ERROR(_logger, "This is a long(hex): " << std::hex << 100000000) LOG4CPLUS_FATAL(_logger, "This is a double: " << std::setprecision(15) << 1.2345234234) return 0; } 输出结果: DEBUG - This is a bool: 1 INFO - This is a char: x WARN - This is a int: 1000 ERROR - This is a long(hex): 5f5e100 FATAL - This is a double: 1.2345234234 〖例4〗 /* 调试模式,通过loglog来控制输出调试、警告或错误信息,appender输出到屏幕。 */ #include #include using namespace log4cplus::helpers; void printMsgs(void) { std::cout << "Entering printMsgs()..." << std::endl; LogLog::getLogLog()->debug("This is a Debug statement..."); LogLog::getLogLog()->warn("This is a Warning..."); LogLog::getLogLog()->error("This is a Error..."); std::cout << "Exiting printMsgs()..." << std::endl << std::endl; } int main() { /* LogLog类实现了debug, warn, error 函数用于输出调试、警告或错误信息, 同时提供了两个方法来进一步控制所输出的信息,其中: setInternalDebugging方法用来控制是否屏蔽输出信息中的调试信息,当输入 参数为false则屏蔽,缺省设置为false。 setQuietMode方法用来控制是否屏蔽所有输出信息,当输入参数为true则屏蔽, 缺省设置为false。 LogLog::getLogLog()->setInternalDebugging(false); */ printMsgs(); std::cout << "Turning on debug..." << std::endl; LogLog::getLogLog()->setInternalDebugging(true); printMsgs(); std::cout << "Turning on quiet mode..." << std::endl; LogLog::getLogLog()->setQuietMode(true); printMsgs(); return 0; } 输出结果: Entering printMsgs()... log4cplus:WARN This is a Warning... log4cplus:ERROR This is a Error... Exiting printMsgs()... Turning on debug... Entering printMsgs()... log4cplus: This is a Debug statement... log4cplus:WARN This is a Warning... log4cplus:ERROR This is a Error... Exiting printMsgs()... Turning on quiet mode... Entering printMsgs()... Exiting printMsgs()... 需要指出的是,输出信息中总是包含"log4cplus:"前缀,有时候会感觉不爽,这是因为LogLog在实现时候死定了要这么写: LogLog::LogLog() : mutex(LOG4CPLUS_MUTEX_CREATE), debugEnabled(false), quietMode(false), PREFIX( LOG4CPLUS_TEXT("log4cplus: ") ), WARN_PREFIX( LOG4CPLUS_TEXT("log4cplus:WARN ") ), ERR_PREFIX( LOG4CPLUS_TEXT("log4cplus:ERROR ") ) { } 你可以把这些前缀换成自己看着爽的提示符号,然后重新编译,hihi。除非万不得已或者实在郁闷的不行,否则还是不要这样干。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yefei123123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值