向量<对<双精度,双精度>>给出问题
vector<pair<double,double>> giving problems
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<pair<double, double> > Vec; //cords vector (0.0,0.0)/(x,y)
typedef pair<double, double> Punto; //single coord
int main()
{
int x;
cout << "cuants vertex?" << endl;
cin >> x;
Vec V[x]; // Declare vector of pairs as long as user wants
Punto P;
for (int i = 0; i < V.size(); i++) { //fill vector with coords
cout << "Introdueix coordenada numero: " << i;
cout << "x: ";
cin >> P.first;
cout << "y: ";
cin >> P.second;
V.push_back(P);
}
for (int c = 0; c < V.size(); c++) { //show all coords
P.first = V[c].first;
P.second = V[c].second;
cout << P.first << P.second << endl;
}
}
这是一个填充vector<pair<double,double>>
的简单代码,它给出了这个错误:
main.cpp: In function ‘int main()’:
main.cpp:16:18: error: request for member ‘size’ in ‘V’, which is of non-class type ‘Vec [x] {aka std::vector > [x]}’
for(int i=0;i<V.size(); i++){
^~~~
main.cpp:19:5: error: request for member ‘push_back’ in ‘V’, which is of non-class type ‘Vec [x] {aka std::vector > [x]}’
V.push_back(P);
^~~~~~~~~
main.cpp:21:19: error: request for member ‘size’ in ‘V’, which is of non-class type ‘Vec [x] {aka std::vector > [x]}’
for(int c=0; c<V.size(); c++){
^~~~
main.cpp:22:16: error: ‘Vec {aka class std::vector >}’ has no member named ‘first’
P.first=V[c].first;
^~~~~
main.cpp:23:17: error: ‘Vec {aka class std::vector >}’ has no member named ‘second’; did you mean ‘end’?
P.second=V[c].second;
^~~~~~
有什么方法可以解决吗?
这一行:
Vec V[x]; // Declare vector of pairs as long as user wants
实际上是在创建一个C风格的Vec
数组,而不是单个Vec
。您应该改用圆括号,构造单个Vec
:
Vec V(x); // Declare vector of pairs as long as user wants
但是,现在push_back
将添加其他元素。取代
V.push_back(P);
跟
V[i] = P;
Vec V[x];
x
不是常量表达式,因此您声明的是可变长度数组。 这是一个编译器扩展,不是C++。 数组不能有.size()
成员。
您需要将其更改为:
Vec V;
(此外,使用 Vec::size_type
遍历向量,否则您将收到签名警告。
相关文章:
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 正在将csv文件读取为双精度矢量
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 如何在C++中的同一函数中使用字符串和双精度
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 检查是否以特定精度给出双精度
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- C 字符串返回字符串的整数/双精度/长整型值
- 为什么将双精度转换为 int 似乎在第 16 位数字之后将其四舍五入?
- 如何使双精度值的 C++ 和 C# 中的结果相同
- 使用浮点数和双精度数的非常小数字的数学
- 使用 Xcode 将双精度存储在数组C++中
- 在 C++ 中将双精度变量写入二进制文件
- 如何从字符串转换为双精度*
- 为什么我的数组双精度函数不起作用?
- 高精度双精度的 Sprintf 格式化问题
- C++ cout 将双精度对齐到精度 2 并正确对齐
- 将指针数组分配给双精度
- C++如何将字符串逐行转换为双精度
- 长双精度C++是IEEE二进制128的实现吗?