C++标准库开发心得

最近放弃MFC,改用C++标准库开发产品。毕竟MFC用熟了,马上改用STL还不太习惯。下面列出下总结的改用STL遇到的问题和解决办法:

1.清除空格

remove_if(iterBegin, iterEnd, isspace) 会遍历字符串,将空格之后的字符依次往前拷贝,

之后iter为“of MFC.”,为多余字符串的位置,字符个数为空格的个数。需要使用erase将iter部分清除。清除之后字符串为“IuseSTLtodevelopinsteadofMFC.”

#include <string>
#include <algorithm>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    std::string str = "I use STL to develop instead of MFC.";
    std::string::iterator iterBegin = str.begin();
    std::string::iterator iterEnd = str.end();
    std::string::iterator iter = remove_if(iterBegin, iterEnd, isspace);
    str.erase(iter, str.end());
    return 0;
}

原文地址:https://www.cnblogs.com/ityujian/p/3428116.html