初学c++和stl的问题

jhams 2014-05-28 11:25:55
编译c++程序报错:
[root@Host001cbf543fd3 zj]# g++ zj.cpp
zj.cpp: 在函数‘int main()’中:
zj.cpp:17:16: 错误:对‘std::vector<int>::insert(int, int, int)’的调用没有匹配的函数
v.insert(3,5,7);
^
zj.cpp:17:16: 附注:备选是:
In file included from /usr/include/c++/4.8.2/vector:69:0,
from zj.cpp:3:
/usr/include/c++/4.8.2/bits/vector.tcc:107:5: 附注:std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::iterator, const value_type&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = int*; std::vector<_Tp, _Alloc>::value_type = int]
vector<_Tp, _Alloc>::
^
/usr/include/c++/4.8.2/bits/vector.tcc:107:5: 附注: 备选需要 2 实参,但提供了 3 个
In file included from /usr/include/c++/4.8.2/vector:64:0,
from zj.cpp:3:
/usr/include/c++/4.8.2/bits/stl_vector.h:1023:7: 附注:void std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::iterator, std::vector<_Tp, _Alloc>::size_type, const value_type&) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; typename std::_Vector_base<_Tp, _Alloc>::pointer = int*; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::value_type = int]
insert(iterator __position, size_type __n, const value_type& __x)
^
/usr/include/c++/4.8.2/bits/stl_vector.h:1023:7: 附注: no known conversion for argument 1 from ‘int’ to ‘std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}’
/usr/include/c++/4.8.2/bits/stl_vector.h:1050:9: 附注:template<class _InputIterator> void std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::iterator, _InputIterator, _InputIterator) [with _InputIterator = _InputIterator; _Tp = int; _Alloc = std::allocator<int>]
insert(iterator __position, _InputIterator __first,
^
/usr/include/c++/4.8.2/bits/stl_vector.h:1050:9: 附注: template argument deduction/substitution failed:
zj.cpp:17:16: 附注: cannot convert ‘3’ (type ‘int’) to type ‘std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}’
v.insert(3,5,7);

程序是这样的:

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main(void)

{

int i = 0;
vector<int> v;

for( i = 0; i < 10; i++ )
{
v.push_back( i );//把元素一个一个存入到vector中
}
v.insert(3,5,7);
for( i = 0; i < 12; i++ )
{
cout << v.at( i );//把元素一个一个存入到vector中
}
return 0;
}
...全文
206 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-05-28
  • 打赏
  • 举报
回复
请将http://www.cplusplus.com上面所有STL相关例子代码过一遍。
难题 2014-05-28
  • 打赏
  • 举报
回复
看报错啊,insert调用出错,去 看看insert的正确使用方法
shenchenman 2014-05-28
  • 打赏
  • 举报
回复
迭代器是什么好好看看。 v.insert(3,5,7); insert第一个参数是vector的迭代器 参考下面: vector<int>::iterator it; it = it + 2; v.insert(it, 5, 7);
ri_aje 2014-05-28
  • 打赏
  • 举报
回复
insert 第一个参数是迭代器。 你到底要做什么?
jhams 2014-05-28
  • 打赏
  • 举报
回复
弄明白了,谢谢大家!

65,209

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧