最近没啥可写的  这里写下做的STL小练习 作为记录

去除指定字符串中的空格

获取文件名并根据名字创建临时文件,以TMP后缀结尾,已经为TMP后缀结尾文件则创建以XXX后缀结尾文件

读取一行输入内容 并将单词翻转打印

// 1111.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <string>
#include <iostream>
#include <algorithm> using namespace std; string strArray[5] = {"prog.dat","mydir","hello.","oops.tmp","end.dat"}; string TrimSpace(string str)
{ string::size_type i; while( (i = str.find(" ")) != string::npos )
{
str.replace(i,1,"");
} string::iterator newEnd = remove(str.begin(),str.end(),' ');
str.erase(newEnd,str.end()); return str;
} void ReversPrintWordInLine()
{
const string delims(" \t,.;");
string line;
while(getline(cin,line))
{
string::size_type begIdx,endIdx;
begIdx = line.find_first_not_of(delims);
while(begIdx != string::npos)
{
endIdx = line.find_first_of(delims,begIdx);
if(endIdx == string::npos)
{
endIdx = line.length();
}
for(int i = endIdx-1;i >= static_cast<int>(begIdx);--i)
{
cout << line[i];
}
cout << ' '; begIdx = line.find_first_not_of(delims,endIdx);
}
cout << endl;
}
} void CreateTmpFilename()
{ string filename,basename,extname,tmpname;
const string suffix("tmp"); for(int i = 0;i < 5;++i)
{
string::size_type idx = strArray[i].find(".");
if(idx == string::npos)
{
tmpname = strArray[i]+"."+suffix;
}else
{
basename = strArray[i].substr(0,idx);
extname = strArray[i].substr(idx+1);
if(extname.empty())
{
tmpname = strArray[i];
tmpname += suffix;
}else if(extname == suffix)
{
tmpname = strArray[i];
tmpname.replace(idx+1,extname.size(),"xxx");
}else
{
tmpname = strArray[i];
tmpname.replace(idx+1,suffix.size(),suffix);
}
} cout << strArray[i] << " ==> " << tmpname << endl;
}
cout << endl;
} int _tmain(int argc, _TCHAR* argv[])
{
string str = TrimSpace(" sdfsf sdfs sdf ");
cout << "remove space " << str << endl << endl; CreateTmpFilename(); ReversPrintWordInLine(); return 0;
}

  

stl string 小练习的更多相关文章

  1. 深入剖析 linux GCC 4.4 的 STL string

    转自: 深入剖析 linux GCC 4.4 的 STL string 本文通过研究STL源码来剖析C++中标准模板块库std::string运行机理,重点研究了其中的引用计数和Copy-On-Wri ...

  2. 格式字符串分配stl::string

    代码非常easy,不解释,直接在代码: #include <cstdio> #include <cstdarg> #include <iostream> using ...

  3. 浅谈C++ STL string容器

    浅谈C++ STL string容器 本篇随笔简单讲解一下\(C++STL\)中\(string\)容器的使用方法及技巧. string容器的概念 其实\(string\)并不是\(STL\)的一种容 ...

  4. C++标准模板库Stand Template Library(STL)简介与STL string类

    参考<21天学通C++>第15和16章节,在对宏和模板学习之后,开启对C++实现的标准模板类STL进行简介,同时介绍简单的string类.虽然前面对于vector.deque.list等进 ...

  5. 转C++之stl::string写时拷贝导致的问题

    前几天在开发某些数据结构到文件的 Dump 和 Load 功能的时候, 遇到的一个 bug . [问题复现] 问题主要出在 Load 过程中,从文件读取数据的时候, 直接使用 fread 的去操作 s ...

  6. stl string

    10.2.1 STL的string 1String概念 ²  string是STL的字符串类型,通常用来表示字符串.而在使用string之前,字符串通常是用char*表示的.string与char*都 ...

  7. [转] 深入剖析 linux GCC 4.4 的 STL string

    本文通过研究STL源码来剖析C++中标准模板块库std::string运行机理,重点研究了其中的引用计数和Copy-On-Write技术. 平台:x86_64-redhat-linux gcc ver ...

  8. STL - string(典型操作demo)

    1String概念  string是STL的字符串类型,通常用来表示字符串.而在使用string之前,字符串通常是用char*表示的.string与char*都可以用来表示字符串,那么二者有什么区别呢 ...

  9. STL——string

    C++之string类型详解 之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够.字符串长度等等,而且作为一个泛型类出现,他集成的操作函 ...

随机推荐

  1. spring中 的MD5 加密

    //对密码进行加密(不需要使用其他Md5工具  .spring中有 在digestUtils)        String password = DigestUtils.md5DigestAsHex( ...

  2. 7.分工合作include:指定多个配置文件

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 比如让jack来单独开发一个action,在jack.xml中的配置文件为: ...

  3. eclipse 的project explorer问题,这个怎样把localFileSystem去掉

    转自:https://zhidao.baidu.com/question/550279043.html

  4. 前端-CSS-9-文本和字体-背景颜色

    字体属性 div{ width: 300px; height: 100px; /*background-color: red;*/ border: 1px solid red; /*设置字体大小 px ...

  5. 在eclipse中创建maven项目,亲测有效,详细步骤

    一.想要使用maven,首先要配置本地maven的环境 1.在http://maven.apache.org/download.cgi中去下载maven 2. 3.下载完毕后将压缩包解压到自己记住的位 ...

  6. Docker-删除untagged docker images

    故障描述 [root@entel1 ~]# docker rmi entel_zmc_images:zmc_base Untagged: entel_zmc_images:zmc_base 操作步骤 ...

  7. linux 批量删除文件名中有换行符

    ls -i | grep ^M | awk '{print $1}' | xargs -t -I [] find . -inum [] -exec rm -if {} \; 注意^M 是ctrl+v ...

  8. 常用数据库ID格式

    转自:http://www.biotrainee.com/thread-411-1-1.html 常用数据库 ID ID 示例 ID 来源 ENSG00000116717 Ensemble ID GA ...

  9. java写简单Excel 首行是目录 然后前台下载

    页面: <form action="${path}/xxx/xxx.do" method="get" > 表格下载:<input type=& ...

  10. html中的innerHTML

    首先看个例子 <html> <head> <script type="text/javascript"> function getInnerHT ...