C++ C++;-无法读取向量中的最高值
我有一个从txt文件中检索股票市场记录的程序。我将这个文件插入到一个向量中,它可以成功地读取该文件而不会出现任何问题。我想做的是找回最高的股价值 这是我的向量模板:C++ C++;-无法读取向量中的最高值,c++,vector,visual-studio-2015,C++,Vector,Visual Studio 2015,我有一个从txt文件中检索股票市场记录的程序。我将这个文件插入到一个向量中,它可以成功地读取该文件而不会出现任何问题。我想做的是找回最高的股价值 这是我的向量模板: #ifndef H_VectorTemplate #define H_VectorTemplate #include <iostream> #include <string> using namespace std; template <class elemType> class Vect
#ifndef H_VectorTemplate
#define H_VectorTemplate
#include <iostream>
#include <string>
using namespace std;
template <class elemType>
class Vector
{
public:
//default constructor
Vector();
//specific constructor
Vector(int size);
//Destructor
virtual ~Vector();
//insert and print
void insert(const elemType& i);
void print() const;
elemType at(int i) const;
//for dealing with arrays
bool isEmpty() const;
bool isFull() const;
int size() const;
private:
elemType *list;
int length;
int maxsize;
};
//Implementation
template<class elemType>
bool Vector<elemType>::isEmpty() const
{
return (length = 0);
}
template<class elemType>
bool Vector<elemType>::isFull() const
{
return (length = 4000);
}
template<class elemType>
void Vector<elemType>::print() const
{
for (int i = 0; i < length; i++)
{
cout << list[i] << " ";
cout << endl;
}
}
template<class elemType>
Vector<elemType>::Vector() //constructor
{
maxsize = 200;
length = 0;
list = new elemType[maxsize];
}
template<class elemType>
Vector<elemType>::Vector(int size)
{
if (size <= 0)
{
cout << "Invalid size, setting size to 200" << endl;
maxsize = 200;
}
else
maxsize = size;
length = 0;
list = new elemType[maxsize];
}
template<class elemType>
void Vector<elemType>::insert(const elemType& i)
{
if (length == 4000)
cout << "Cannot insert" << endl;
else
{
list[length] = i;
length++;
}
}
template<class elemType>
Vector<elemType>::~Vector()
{
delete []list;
}
template<class elemType>
elemType Vector<elemType>::at(int i) const
{
return list[i];
}
template<class elemType>
int Vector<elemType>::size() const
{
return length;
}
#endif
#ifndef H#u向量模板
#定义H_向量模板
#包括
#包括
使用名称空间std;
模板
类向量
{
公众:
//默认构造函数
向量();
//特定构造函数
向量(整数大小);
//析构函数
虚拟向量();
//插入并打印
无效插入(常量元素类型&i);
无效打印()常量;
元素类型at(int i)const;
//用于处理数组
bool isEmpty()常量;
bool isFull()常量;
int size()常量;
私人:
元素类型*列表;
整数长度;
int-maxsize;
};
//实施
模板
布尔向量::isEmpty()常量
{
返回(长度=0);
}
模板
布尔向量::isFull()常量
{
返回(长度=4000);
}
模板
void Vector::print()常量
{
for(int i=0;i coutlength=0
错误,请使用等于=运算符
template<class elemType>
bool Vector<elemType>::isEmpty() const
{
return (length == 0);
}
template<class elemType>
bool Vector<elemType>::isFull() const
{
return (length == 4000);
}
V1.at(j).getPrice==HP
错误:使用getPrice()调用它
if (V1.at(j).getPrice() == HP)
getDateTime
和getPrice
是函数。您需要用括号调用它们:getDateTime()
和getPrice()
。这只是一个打字错误,不值得问一个完整的问题。。啊,对不起,我的错误。我仍然将“=”函数作为左操作数错误。我也不能使用“==”。因为它说从双精度到双精度没有转换。@bumpfox“从双精度到双精度没有转换”-我打赌它没有这么说。比这多一点。这里:好眼睛。没有看到那些。这很有效,非常感谢。很抱歉给您带来麻烦。另外,避免将像4000
这样的神奇文字硬编码到函数中,在别处将它们声明为常量或宏。
template<class elemType>
bool Vector<elemType>::isFull() const
{
return (length == 4000);
}
if (V1.at(j).getPrice() == HP)