字符串类型末尾减一个字符怎么弄?
比如定义一个字符串类型为string a;a="today is sunday";
想把a变为today is sunda,有没有什么快捷的方法?
2016-08-31 07:47
Removes the last character from the string. Equivalent to erase(size()-1, 1), except the behavior is undefined if the string is empty.
2016-08-31 08:25
2016-08-31 09:08
程序代码:#include <conio.h> // vc only 非标准的东西
#include <cstdio>
#include <string>
std::string GetPassord( void )
{
// 清除之前遗留的所有输入
for( ; _kbhit(); ) _getch();
std::string pwd;
for( int ch; ch=_getch(), ch!='\r'; )
{
if( ch==0 || ch==0xE0 ) // 功能键
_getch();
else if( ch == 0x1B ) // ESC键
{
for( size_t i=0; i!=pwd.size(); ++i )
printf( "\b \b" );
pwd.clear();
}
else if( ch == '\b' ) // 退格键
{
if( !pwd.empty() )
{
printf( "\b \b" );
pwd.erase( pwd.size()-1 );
}
}
else if( ch>=0x20 && ch<0x7F ) // 可显字符
{
putchar( '*' );
pwd += ch;
}
}
// 清除之前遗留的所有输入
for( ; _kbhit(); ) _getch();
putchar( '\n' );
return pwd;
}
int main( void )
{
printf( "%s", "Enter password: " );
std::string pwd = GetPassord();
// puts( pwd.c_str() );
if( pwd != "eb36520" )
{
puts( "Password error\n" );
return 1;
}
puts( "\nDo Well\n" );
return 0;
}
2016-08-31 10:29