如何在c ++中使用vector [英] How to use vector in c++

查看:91
本文介绍了如何在c ++中使用vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,

我正在使用c ++中的矢量,我真的不太理解矢量

i想在终端中显示以下内容



[]

[0 0 0]

错误消息

错误消息

[1 2 3]

[1 2 3]

[1 0 3}



这是我的Vector.cpp文件

Hey,
I'm working on vector in c++, and i really don't understand vector very well
i would like to display the following in the terminal

[]
[0 0 0]
error message
error message
[1 2 3]
[1 2 3 ]
[1 0 3 }

here's my Vector.cpp file

#include "Vector.h"
#include <iostream>
#include <vector>

using namespace std;
int main() {

	vector<double> a, b(3), c(3);

a.print ();   //outputs []
b.print ();   // outputs [0 0 0]
c.set(0,-1):   //output error message
c.set(1,0):
c.set(2,1):
c.set(3,2):
c.set(4,3):         /output error message
c.print();           //output [1 2 3]

vector d(c);     
d.print();           //outputs [1 2 3 ] 

d.set(0,1);   
d.print();         // outputs [1 0  3 }

return 0 ; 
}





这里是我的Vector.h文件





and here's my Vector.h file

#ifndef VECTOR_H
#define VECTOR_H
#define NULL 0
#include <iostream>

using namespace std;
class Vector
{
public:
	Vector::Vector() {
		size = 0;
		enteries = new int[size];
		for (int k = 0; k < size; k++) {

			enteries[k] = 0;
		}
	}
	Vector::Vector(int s) {
		size = s;
		enteries = new int[size];
		for (int L = 0; L < size; L++) {
			enteries[L] = 0;
		}
	}

	Vector::Vector(const Vector & other) {
		this->size = other.size;
		this->enteries = new int[size];
		for (int i = 0; i < size; i++) {

			this->enteries[i] = other.enteries[i];
		}
	}

	Vector::~Vector() {
		this->enteries = NULL;
	}
	void Vector::print() {
		cout << endl << "[";
		for (int i = 0; i < size; i++) {
			cout << enteries[i] << " ";
		}
		cout << "]" << endl;
	}
	void Vector::set(int val, int pos) {
		if (pos < 0|| pos < size) {
			cout << endl << "Error Message ";
		}
	}


private:
	int size; 
	int *enteries; 
};
#endif





我尝试了什么:



我已经做了头文件,但是我真的不知道如何在运行程序后显示以下内容

[]

[0 0 0]

错误消息

错误消息

[1 2 3 ]

[1 2 3]

[1 0 3}

感谢您的帮助。



What I have tried:

I have already did the header file, but i really don't know how to display the following after running the program
[]
[0 0 0]
error message
error message
[1 2 3]
[1 2 3 ]
[1 0 3 }
Thank you for your help.

推荐答案

set()中的大小检查错误,您没有分配值:

The size checking in set() is wrong and you are not assigning the value:
void Vector::set(int val, int pos) {
    // This is wrong:
    //if (pos < 0|| pos < size) {
    // It should be:
    if (pos < 0 || pos >= size) {
        cout << endl << "Error Message ";
    }
    // This is missing:
    else
        enteries[pos] = val;
}





在析构函数中,你应该删除分配的内存。将指针设置为 NULL 是没用的:



In your destructor you should delete the allocated memory. Setting the pointer to NULL is useless:

Vector::~Vector() {
    // This is missing:
    delete [] this->enteries;
    // This is useless:
    //this->enteries = NULL;
}


除了解决方案1:如果你在标题中定义 Vector 文件你应该在你的cpp文件中使用 Vector (注意大写字母V),而不是 vector
And in addition to solution 1: If you define Vector in your header file you should use Vector in your cpp file (note the capital V), and not vector!


这篇关于如何在c ++中使用vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆