久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從 C++ 中的 std::string 中刪除空格

Remove spaces from std::string in C++(從 C++ 中的 std::string 中刪除空格)
本文介紹了從 C++ 中的 std::string 中刪除空格的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

在 C++ 中從字符串中刪除空格的首選方法是什么?我可以遍歷所有字符并構(gòu)建一個新字符串,但有沒有更好的方法?

What is the preferred way to remove spaces from a string in C++? I could loop through all the characters and build a new string, but is there a better way?

推薦答案

最好的做法是使用算法 remove_if 和 isspace:

The best thing to do is to use the algorithm remove_if and isspace:

remove_if(str.begin(), str.end(), isspace);

現(xiàn)在算法本身不能改變?nèi)萜?只能修改值),所以它實際上將值打亂并返回一個指向現(xiàn)在結(jié)束位置的指針.所以我們必須調(diào)用string::erase來實際修改容器的長度:

Now the algorithm itself can't change the container(only modify the values), so it actually shuffles the values around and returns a pointer to where the end now should be. So we have to call string::erase to actually modify the length of the container:

str.erase(remove_if(str.begin(), str.end(), isspace), str.end());

我們還應(yīng)該注意,remove_if 最多只會制作一份數(shù)據(jù)副本.這是一個示例實現(xiàn):

We should also note that remove_if will make at most one copy of the data. Here is a sample implementation:

template<typename T, typename P>
T remove_if(T beg, T end, P pred)
{
    T dest = beg;
    for (T itr = beg;itr != end; ++itr)
        if (!pred(*itr))
            *(dest++) = *itr;
    return dest;
}

這篇關(guān)于從 C++ 中的 std::string 中刪除空格的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

boost_1_60_0 .zip installation in windows(Windows 中的 boost_1_60_0 .zip 安裝)
How do I get console output in C++ with a Windows program?(如何使用 Windows 程序在 C++ 中獲得控制臺輸出?)
How do I calculate the week number given a date?(如何計算給定日期的周數(shù)?)
OpenCV with Network Cameras(帶有網(wǎng)絡(luò)攝像機的 OpenCV)
Export all symbols when creating a DLL(創(chuàng)建 DLL 時導(dǎo)出所有符號)
Getting started with OpenCV 2.4 and MinGW on Windows 7(Windows 7 上的 OpenCV 2.4 和 MinGW 入門)
主站蜘蛛池模板: 欧美a在线| 91一区| 成人亚洲性情网站www在线观看 | 久久国产精品精品 | 免费中文字幕日韩欧美 | 欧美久| 欧美成人精品一区二区三区 | 久久久久亚洲精品 | 狠狠干网| 精品一区二区电影 | 欧美成人激情视频 | 久久久久欧美 | 一级片毛片 | 国产69精品久久久久777 | 91 在线| 91av亚洲| 成人欧美一区二区三区 | 久久国| 草在线| 中文av字幕 | 久久乐国产精品 | 久久亚洲精品国产精品紫薇 | 久久五月婷 | 国产精品美女久久久 | 毛片高清| 亚洲综合一区二区三区 | 日本免费网 | 国产福利资源在线 | 福利网站在线观看 | 91精品久久久久久久久中文字幕 | 丁香综合| 91视频精选| 成人夜晚看av| 中文字幕乱码一区二区三区 | 国产日本精品视频 | 亚洲黄色在线免费观看 | 亚洲精品乱码久久久久久9色 | 久久久久国产精品一区二区 | 成人一区二区三区 | 亚洲性视频 | 国产一区二区在线播放 |