将字符串映射到无符号 int 到无符号 int 的映射
Mapping string to map of unsigned int to unsigned int
我试图将cin中的每个单词映射到单词出现的行号,以及它出现在该行上的次数。
我不确定我的循环是否有效。我想我对地图有所了解,但我不是 100% 确定这是否有效,我无法打印它进行测试,因为我还没有弄清楚应该如何打印它。我的问题是,我的地图看起来还行吗?
int main ( int argc, char *argv[] )
{
map<string, map<unsigned int, unsigned int> > table;
unsigned int linenum = 1;
string line;
while ( getline(cin, line) != cin.eof()){
istringstream iss(line);
string word;
while(iss >> word){
++table[word][linenum];
}
linenum++;
}
while ( getline(cin, line) != cin.eof() ){
/*~~~~~~~~~~~~ Don't use this,
the comparison is incorrect */
要打印它,只需循环访问您的地图:
for(const auto& x:table)
{
std::cout << x.first << ' ';
for(const auto& y:x.second)
{
std::cout << y.first << ":" << y.second << ' ';
}
std::cout << std::endl;
}
见here
对于 C++98 使用:
for(mIt x = table.begin();
x != table.end();
++x )
{
std::cout << x->first << ' ' ;
for( It y = x->second.begin();
y != x->second.end();
++y )
{
std::cout << y->first << ":" << y->second << ' ';
}
std::cout << std::endl;
}
相关文章:
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 在 std::无符号字符的向量处存储 int 的十六进制表示形式
- 从 std::vector<无符号字符>切片中提取 int?
- 是什么导致了这种使用三进制而不是短整型的有符号int到无符号int转换
- C++无符号短裤的划分导致 int
- 将无符号转换为复杂<int>原因符号转换警告
- 如何解决隐式转换丢失整数精度:'size_t'(又名"无符号长")到'int'警告?
- 无符号的 int 到有效数字
- C++:使用没有位移位的指针将无符号字符转换为无符号 int
- 整数类型应该显式转换(例如"int"到"无符号")还是只会增加混乱?
- 有没有办法在C++中将mpz_t转换为 int 或无符号长 int?
- 是否可以将无符号 int 的最大值转换为 int 并将结果转换为 -1?
- 为什么QByteArray的大小是"int"而不是"无符号int"
- 为什么 (-i) 的类型(其中 i 是无符号的 int)仍然是无符号的 int?
- 无符号长整型和无符号 int 之间有什么区别,这 2 种类型应该如何在 c# 中封送?
- 如何对无符号长 int 进行位掩码?
- 使用无符号int作为二进制来在c++中实现一个集
- 这个右移是如何工作的:字符串流>>无符号的int>>无符号的int?
- 转换 int -> 无符号长 长 是否由标准定义
- 为什么在执行 int / 无符号 int 除法时使用 'divl'