鱼C论坛

 找回密码
 立即注册
查看: 5656|回复: 5

帮看看为什么错了

[复制链接]
发表于 2013-3-8 21:04:49 | 显示全部楼层 |阅读模式
5鱼币
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5. int main()
  6. {
  7.         vector<string> svect;//字符容器
  8.        
  9.         //输入单词
  10.         for (vector<string>::size_type i=0;cin>>svect[i++];)
  11.         {
  12.         }
  13.         //负责对单词计数
  14.         vector<int> ivect(0);
  15.         //进行相同单词计数
  16.         for (vector<int>::iterator i=svect.begin();i!=svect.end();++i)
  17.         {
  18.                 for (vector<int>::iterator j=svect.begin();j!=svect.end();++j)
  19.                 {
  20.                         //如果相等就加一
  21.                         if (svect[i]==svect[j])
  22.                         {
  23.                                 ivect[i]+=1;
  24.                         }
  25.                 }
  26.         }
  27.         //选出最多的次数
  28.         int max_i=ivect[0];
  29.         vector<int>::size_type num=0;
  30.         for(vector<int>::iterator i=ivect.begin()+1;i!=ivect.end();i++)
  31.         {
  32.                 num=(max_i<ivect[i]) ?i:num;
  33.         }
  34.         //如果不是零则输出单词和个数
  35.         if(max_i!=0)
  36.         {
  37.                 cout<<svect[num]<<"has"<<num<<endl;
  38.         }
  39.         return 0;
  40. }
复制代码
  1. 错误

  2. 1>------ 已启动生成: 项目: 测试, 配置: Debug Win32 ------
  3. 1>正在编译...
  4. 1>1.cpp
  5. 1>g:\资源\c\新建文件夹\win32 console application\测试\测试\1.cpp(16) : error C2440: “初始化”: 无法从“std::_Vector_iterator<_Ty,_Alloc>”转换为“std::_Vector_iterator<_Ty,_Alloc>”
  6. 1>        with
  7. 1>        [
  8. 1>            _Ty=std::string,
  9. 1>            _Alloc=std::allocator<std::string>
  10. 1>        ]
  11. 1>        and
  12. 1>        [
  13. 1>            _Ty=int,
  14. 1>            _Alloc=std::allocator<int>
  15. 1>        ]
  16. 1>        无构造函数可以接受源类型,或构造函数重载决策不明确
  17. 1>g:\资源\c\新建文件夹\win32 console application\测试\测试\1.cpp(16) : error C2679: 二进制“!=”: 没有找到接受“std::_Vector_iterator<_Ty,_Alloc>”类型的右操作数的运算符(或没有可接受的转换)
  18. 1>        with
  19. 1>        [
  20. 1>            _Ty=std::string,
  21. 1>            _Alloc=std::allocator<std::string>
  22. 1>        ]
  23. 1>        h:\新建文件夹 (1)\vc\include\vector(214): 可能是“bool std::_Vector_const_iterator<_Ty,_Alloc>::operator !=(const std::_Vector_const_iterator<_Ty,_Alloc> &) const”
  24. 1>        with
  25. 1>        [
  26. 1>            _Ty=int,
  27. 1>            _Alloc=std::allocator<int>
  28. 1>        ]
  29. 1>        试图匹配参数列表“(std::_Vector_iterator<_Ty,_Alloc>, std::_Vector_iterator<_Ty,_Alloc>)”时
  30. 1>        with
  31. 1>        [
  32. 1>            _Ty=int,
  33. 1>            _Alloc=std::allocator<int>
  34. 1>        ]
  35. 1>        and
  36. 1>        [
  37. 1>            _Ty=std::string,
  38. 1>            _Alloc=std::allocator<std::string>
  39. 1>        ]
  40. 1>g:\资源\c\新建文件夹\win32 console application\测试\测试\1.cpp(18) : error C2440: “初始化”: 无法从“std::_Vector_iterator<_Ty,_Alloc>”转换为“std::_Vector_iterator<_Ty,_Alloc>”
  41. 1>        with
  42. 1>        [
  43. 1>            _Ty=std::string,
  44. 1>            _Alloc=std::allocator<std::string>
  45. 1>        ]
  46. 1>        and
  47. 1>        [
  48. 1>            _Ty=int,
  49. 1>            _Alloc=std::allocator<int>
  50. 1>        ]
  51. 1>        无构造函数可以接受源类型,或构造函数重载决策不明确
  52. 1>g:\资源\c\新建文件夹\win32 console application\测试\测试\1.cpp(18) : error C2679: 二进制“!=”: 没有找到接受“std::_Vector_iterator<_Ty,_Alloc>”类型的右操作数的运算符(或没有可接受的转换)
  53. 1>        with
  54. 1>        [
  55. 1>            _Ty=std::string,
  56. 1>            _Alloc=std::allocator<std::string>
  57. 1>        ]
  58. 1>        h:\新建文件夹 (1)\vc\include\vector(214): 可能是“bool std::_Vector_const_iterator<_Ty,_Alloc>::operator !=(const std::_Vector_const_iterator<_Ty,_Alloc> &) const”
  59. 1>        with
  60. 1>        [
  61. 1>            _Ty=int,
  62. 1>            _Alloc=std::allocator<int>
  63. 1>        ]
  64. 1>        试图匹配参数列表“(std::_Vector_iterator<_Ty,_Alloc>, std::_Vector_iterator<_Ty,_Alloc>)”时
  65. 1>        with
  66. 1>        [
  67. 1>            _Ty=int,
  68. 1>            _Alloc=std::allocator<int>
  69. 1>        ]
  70. 1>        and
  71. 1>        [
  72. 1>            _Ty=std::string,
  73. 1>            _Alloc=std::allocator<std::string>
  74. 1>        ]
  75. 1>g:\资源\c\新建文件夹\win32 console application\测试\测试\1.cpp(21) : error C2679: 二进制“[”: 没有找到接受“std::_Vector_iterator<_Ty,_Alloc>”类型的右操作数的运算符(或没有可接受的转换)
  76. 1>        with
  77. 1>        [
  78. 1>            _Ty=int,
  79. 1>            _Alloc=std::allocator<int>
  80. 1>        ]
  81. 1>        h:\新建文件夹 (1)\vc\include\vector(758): 可能是“const std::basic_string<_Elem,_Traits,_Ax> &std::vector<_Ty>::operator [](unsigned int) const”
  82. 1>        with
  83. 1>        [
  84. 1>            _Elem=char,
  85. 1>            _Traits=std::char_traits<char>,
  86. 1>            _Ax=std::allocator<char>,
  87. 1>            _Ty=std::string
  88. 1>        ]
  89. 1>        h:\新建文件夹 (1)\vc\include\vector(773): 或       “std::basic_string<_Elem,_Traits,_Ax> &std::vector<_Ty>::operator [](unsigned int)”
  90. 1>        with
  91. 1>        [
  92. 1>            _Elem=char,
  93. 1>            _Traits=std::char_traits<char>,
  94. 1>            _Ax=std::allocator<char>,
  95. 1>            _Ty=std::string
  96. 1>        ]
  97. 1>        试图匹配参数列表“(std::vector<_Ty>, std::_Vector_iterator<_Ty,_Alloc>)”时
  98. 1>        with
  99. 1>        [
  100. 1>            _Ty=std::string
  101. 1>        ]
  102. 1>        and
  103. 1>        [
  104. 1>            _Ty=int,
  105. 1>            _Alloc=std::allocator<int>
  106. 1>        ]
  107. 1>g:\资源\c\新建文件夹\win32 console application\测试\测试\1.cpp(21) : error C2679: 二进制“[”: 没有找到接受“std::_Vector_iterator<_Ty,_Alloc>”类型的右操作数的运算符(或没有可接受的转换)
  108. 1>        with
  109. 1>        [
  110. 1>            _Ty=int,
  111. 1>            _Alloc=std::allocator<int>
  112. 1>        ]
  113. 1>        h:\新建文件夹 (1)\vc\include\vector(758): 可能是“const std::basic_string<_Elem,_Traits,_Ax> &std::vector<_Ty>::operator [](unsigned int) const”
  114. 1>        with
  115. 1>        [
  116. 1>            _Elem=char,
  117. 1>            _Traits=std::char_traits<char>,
  118. 1>            _Ax=std::allocator<char>,
  119. 1>            _Ty=std::string
  120. 1>        ]
  121. 1>        h:\新建文件夹 (1)\vc\include\vector(773): 或       “std::basic_string<_Elem,_Traits,_Ax> &std::vector<_Ty>::operator [](unsigned int)”
  122. 1>        with
  123. 1>        [
  124. 1>            _Elem=char,
  125. 1>            _Traits=std::char_traits<char>,
  126. 1>            _Ax=std::allocator<char>,
  127. 1>            _Ty=std::string
  128. 1>        ]
  129. 1>        试图匹配参数列表“(std::vector<_Ty>, std::_Vector_iterator<_Ty,_Alloc>)”时
  130. 1>        with
  131. 1>        [
  132. 1>            _Ty=std::string
  133. 1>        ]
  134. 1>        and
  135. 1>        [
  136. 1>            _Ty=int,
  137. 1>            _Alloc=std::allocator<int>
  138. 1>        ]
  139. 1>g:\资源\c\新建文件夹\win32 console application\测试\测试\1.cpp(23) : error C2679: 二进制“[”: 没有找到接受“std::_Vector_iterator<_Ty,_Alloc>”类型的右操作数的运算符(或没有可接受的转换)
  140. 1>        with
  141. 1>        [
  142. 1>            _Ty=int,
  143. 1>            _Alloc=std::allocator<int>
  144. 1>        ]
  145. 1>        h:\新建文件夹 (1)\vc\include\vector(758): 可能是“const int &std::vector<_Ty>::operator [](unsigned int) const”
  146. 1>        with
  147. 1>        [
  148. 1>            _Ty=int
  149. 1>        ]
  150. 1>        h:\新建文件夹 (1)\vc\include\vector(773): 或       “int &std::vector<_Ty>::operator [](unsigned int)”
  151. 1>        with
  152. 1>        [
  153. 1>            _Ty=int
  154. 1>        ]
  155. 1>        试图匹配参数列表“(std::vector<_Ty>, std::_Vector_iterator<_Ty,_Alloc>)”时
  156. 1>        with
  157. 1>        [
  158. 1>            _Ty=int
  159. 1>        ]
  160. 1>g:\资源\c\新建文件夹\win32 console application\测试\测试\1.cpp(32) : error C2679: 二进制“[”: 没有找到接受“std::_Vector_iterator<_Ty,_Alloc>”类型的右操作数的运算符(或没有可接受的转换)
  161. 1>        with
  162. 1>        [
  163. 1>            _Ty=int,
  164. 1>            _Alloc=std::allocator<int>
  165. 1>        ]
  166. 1>        h:\新建文件夹 (1)\vc\include\vector(758): 可能是“const int &std::vector<_Ty>::operator [](unsigned int) const”
  167. 1>        with
  168. 1>        [
  169. 1>            _Ty=int
  170. 1>        ]
  171. 1>        h:\新建文件夹 (1)\vc\include\vector(773): 或       “int &std::vector<_Ty>::operator [](unsigned int)”
  172. 1>        with
  173. 1>        [
  174. 1>            _Ty=int
  175. 1>        ]
  176. 1>        试图匹配参数列表“(std::vector<_Ty>, std::_Vector_iterator<_Ty,_Alloc>)”时
  177. 1>        with
  178. 1>        [
  179. 1>            _Ty=int
  180. 1>        ]
  181. 1>生成日志保存在“file://g:\资源\c\新建文件夹\win32 console application\测试\测试\Debug\BuildLog.htm”
  182. 1>测试 - 8 个错误,0 个警告
  183. ========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
复制代码


最佳答案

查看完整内容

一、string向量的迭代器应该也是string的 二、向量的迭代器不能作为向量的下标 三、向量不能直接输入
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-3-8 21:04:50 | 显示全部楼层
本帖最后由 wangyexin 于 2013-3-8 22:07 编辑

一、string向量的迭代器应该也是string的
二、向量的迭代器不能作为向量的下标
三、向量不能直接输入


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-3-8 22:21:53 | 显示全部楼层
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5. int main()
  6. {
  7.         vector<string> svect;//字符容器
  8.         
  9.         //输入单词
  10.         string str;
  11.         while(cin>>str)
  12.         {
  13.                         svect.push_back(str);       
  14.                 }
  15.         /*
  16.         for (vector<string>::size_type i=0;cin>>svect[i++];)
  17.         {
  18.         }*/
  19.         //负责对单词计数
  20.         //vector<int> ivect(0); //这是初始化一个长度为零的向量
  21.         vector<int> ivect(svect.size(),0); //初始化一个长度为svect.size(),值为0
  22.         
  23.         //进行相同单词计数
  24.         for (int i = 0 ; i<svect.size() ; ++i)
  25.         {
  26.                 for (int j = 0 ; j<svect.size() ; ++j)
  27.                 {
  28.                         //如果相等就加一
  29.                         if (svect[i] == svect[j])
  30.                         {
  31.                                 ivect[i]+=1;
  32.                         }
  33.                 }
  34.         }
  35.         
  36.         //选出最多的次数
  37.         int max_i=ivect[0];  cout<<max_i<<endl;//
  38.         vector<int>::size_type num=0;
  39.         for(int i = 0 ; i<svect.size() ; ++i)
  40.         {
  41.                // num=(max_i<ivect[i]) ?i:num;
  42.                //这里如果是这样的话 max应该更新
  43.                            max_i = (max_i > ivect[i]) ? max_i : ivect[i];
  44.                
  45.         }
  46.         //如果不是零则输出单词和个数
  47.         if(max_i!=0)
  48.         {
  49.                // cout<<svect[num]<<" has "<<num<<endl;
  50.                cout<<svect[num]<<" has "<<max_i<<endl;
  51.         }
  52.         return 0;
  53. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-3-11 16:41:33 | 显示全部楼层

太感谢,不过我还是有个地方不明白
  1. for (vector<string>::iterator i=svect.begin();i!=svect.end();++i)
  2.         {
  3.                 for (vector<string>::iterator j=svect.begin();j!=svect.end();++j)
  4.                 {
  5.                         //如果相等就加一
  6.                         if (svect[i]==svect[j])
  7.                         {
  8.                                 ivect[i]+=1;
  9.                         }
  10.                 }
  11.         }[code]for(vector<int>::iterator i=ivect.begin()+1;i!=ivect.end();i++)
  12.         {
  13.                 num=(max_i<ivect[i]) ?i:num;
  14.                 max_i=(max_i<ivect[i])?ivect[i]:max_i;
  15.         }
复制代码
[/code]
这两个块中为什么有迭代器就会出现
error 2679 二进制“[”: 没有找到接受“std::_Vector_iterator<_Ty,_Alloc>”类型的右操作数的运算符(或没有可接受的转换)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-3-11 18:57:00 | 显示全部楼层
gwcome 发表于 2013-3-11 16:41
太感谢,不过我还是有个地方不明白[/code]
这两个块中为什么有迭代器就会出现
error 2679 二进制“[”: ...

svect[i]这里i应该是整数类型,但是你定义vector<string>::iterator i,
i不是整数类型,所以报错
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-3-11 19:51:04 | 显示全部楼层
仰望天上的光 发表于 2013-3-11 18:57
svect这里i应该是整数类型,但是你定义vector::iterator i,
i不是整数类型,所以报错

svect是储存string类型的容器,迭代器应该是用vector<string>::iterator类定义吧????要不然程序怎么知道跳几个字节到下一个元素呢,和指针类似吧?大版主求教
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-8-7 04:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表