我正在编写一个将随机字符串作为输入的算法。我需要输出遇到的最大长度以及有多少单词具有该特定长度。是的,我知道我可以使用不同的方法,但我想将每个字符串存储在 av[it's length] 中,如果有多个长度相同的字符串,那么我会增加 u[same length],然后只输出这些值。如何检查 v[length] 是否已设置?
抱歉,可能英语不好。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
typedef unsigned short us;
typedef vector<string> vstr;
typedef vector<us> vus;
int main()
{
string str;
vstr v;
vus u(50, 1);
us len;
while (cin >> str && str != "0")
{
len = str.length();
//if there is an element set at v[len] then ++u[len] otherwise v[len] = str;
}
//bunch of other code
return 0;
}