posts - 134,  comments - 239,  trackbacks - 0
我初学,莫笑。呵呵!

1.去除字符串前后空格:

string trim(string &str)
{
    
if (str.empty()) 
    {
        
return str;
    }
    str.erase(
0,str.find_first_not_of(" "));
    str.erase(str.find_last_not_of(
" "+ 1);
    
return str;
}

当然去除前端空格,或者去除末尾空格都已经明了。

2.文本文件读取,文件内容键值对 ,以‘=’分隔:

    string strtmp;
    ifstream fs(
"Test.txt");
    map
<string,string>  map_temp;
    
string::size_type index = string::npos;

    
while(getline(fs,strtmp,'\n'))
    {
        index 
= strtmp.find("=");
        map_temp[trim(strtmp.substr(
0,index-1))] = trim(strtmp.substr(++index));
    }
    map
<string,string> ::iterator it = map_temp.begin();

    
for(; it != map_temp.end(); it++)
    {
        cout 
<< it->first << "=" << it->second << endl;
    }

注意:
string s = "aa=" ; 其中 ‘=’索引为2,s.substr(0,1) 返回的将是"aa".




posted on 2008-07-24 01:36 -274°C 阅读(56) 评论(0)  编辑  收藏 所属分类: C++

标题  
姓名  
主页
验证码 *  
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
 
 
相关链接:
网站导航:

<2008年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

常用链接

留言簿(11)

随笔分类(139)

随笔档案(134)

JAVA网站

关注的Blog

最新随笔

搜索

  •  

积分与排名

  • 积分 - 80943
  • 排名 - 98

最新评论

阅读排行榜

评论排行榜