打印向量元素错误消息
printing vector elements error message
我是C 的初学者。以下简单的程序将元素推入向量并在遍历时显示它们,这是无法正常工作的。编译器说:"表达:矢量迭代器是不可解释的。"有人可以帮助我吗?
#include <iostream>
#include<vector>
int main()
{
int n;
std::vector<int>x;
std::vector<int>::iterator it;
for (it = x.begin(); it != x.end(); ++it)
{
std::cout << "enter an element in the vector:" << "n";
std::cin >> n;
x.push_back(n);
std::cout << "vector:" << "n";
std::cout << *it;
}
return 0;
}
选择了两个迭代器,即IT1和IT2变量。IT1用于将输入存储到向量中。向量的初始输入在此处存储。
std::vector<int>::iterator it1;
it1=x.begin();
std::cout << "enter an element in the vector: " << "n";
std::cin >> n;
x.push_back(n);
要退出循环,请输入'-1'
while(n!=-1)
{
++it1;
std::cout << "enter an element in the vector: " << "n";
std::cin >> n;
x.push_back(n);
} ;
IT2用于显示向量的输出。
for(it2 = x.begin(); it2 != x.end(); ++it2)
std::cout<<*it2<<" ";//no need of curly braces for single for statement
'完成工作计划:
#include <iostream>
#include<vector>
int main()
{
int n;
std::vector<int> x;
std::vector<int>::iterator it1;
std::vector<int>::iterator it2;
it1=x.begin();
std::cout << "enter an element in the vector: " << "n";
std::cin >> n;
x.push_back(n);
while(n!=-1)
{
++it1;
std::cout << "enter an element in the vector: " << "n";
std::cin >> n;
x.push_back(n);
};
std::cout<<"Vector : ";
for(it2 = x.begin(); it2 != x.end(); ++it2)
std::cout<<*it2<<" " ;
return 0;
}
相关文章:
- boost::进程间消息队列引发错误
- C++错误消息*成员参考.**初学者*
- 如何通过参数抛出错误消息
- glad 导致 glfwSwapBuffers 返回错误消息
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 重新定义预定义的 errno 错误消息 (E2BIG)
- SDL 映像:无法打开映像,仅显示错误消息
- 错误消息:使用"string* +="后"no match for 'operator+='"
- 使用公钥加密消息:BER 解码错误
- 我在主函数的左括号上不断收到错误,消息为obj\Debug\main.o||在函数"ZN11linked_listC1Ev"中:|
- 错误消息"expected expression"....有人知道它为什么这么说吗?
- 在 Eclipse: "error: no match for 'operator='" 中获取错误消息
- 出现分段错误时,更改显示的消息错误
- 使用C 中的OPENSL RSA函数解密消息错误
- Linux汇编程序消息错误c++
- C++写入消息错误
- 发送http post消息错误报告的安全方式
- C++,在尝试创建类时收到此消息:错误:没有在类“media”中声明的“void media::*()”成员函数
- 编译器错误消息错误:令牌之前的预期')' '*'。我的构造函数出错