|
发表于 2023-1-10 16:49:22
|
显示全部楼层
#include
using
namespace
std;
const
int
max
=
20;
int
main()
{
unsigned
int
maxlen
=
0;
int
len
=
0;
int
i=0;
string
str[max];
cout
<<
"请输入一段字符串(ctrl+z)完成输入:\n";
getline(cin,str[i],'
');
while
(cin)
{
if
(maxlen
<
str[i].size())
{
maxlen
=
str[i].size();
len
=
i;
}
++i;
getline(cin,str[i],'
');
}
cout
<<
"最长字符串味:\n";
cout
<<
"第
"
<<
i
<<
"个,字符长度为:"
<<
maxlen
-
1
<<
"
\n字符串为:"
<<
str[len]
<<
endl;
system("pause");
}
下面是使用了vector
的方法
#include
#include
#include
using
namespace
std;
int
main()
{
vector
sv;
string
str;
int
maxlen
=
0;
int
len
=
0;
cout
<<
"请输入一段字符串(ctrl+z)完成输入:\n";
getline(cin,str,'
');
while
(cin)
{
sv.push_back(str);
if
(maxlen
<
str.size())
{
maxlen
=
str.size();
len
=
sv.size();
}
getline(cin,str,'
');
}
cout
<<
"共有"
<<
sv.size()
<<
"个词组\n"
;
cout
<<
"最长字符串味:\n";
cout
<<
"第
"
<<
len
<<
"个,字符长度为:"
<<
maxlen
<<
"
\n字符串为:"
<<
***.at(len-1)
<<
endl;
system("pause");
} |
|