我初学,莫笑。呵呵!
1.去除字符串前后空格:
当然去除前端空格,或者去除末尾空格都已经明了。
2.文本文件读取,文件内容键值对 ,以‘=’分隔:
注意:
string s = "aa=" ; 其中 ‘=’索引为2,s.substr(0,1) 返回的将是"aa".
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;
}
{
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;
}
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".