0

Hi,
I've just started to learn vectors and wonder if there is a way to copy a vector that i use in main to a vector in a class??

5
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by winry
0
// vector.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <iostream>
using namespace std ;

#include <vector>
using std::vector;



class ClassVectorPassing
{
const vector<int> my_vector  ;
public:	
	
	
	// constructor takes the vector
	ClassVectorPassing ( vector<int> & youVector ) :my_vector ( youVector ) 
	{
		 
	}

	
	
	void printVector ()
	{
		for ( int i = 0 ; i < my_vector.size() ; i++)
		{
			cout << my_vector.at(i) << endl ;
		}


	}


};




int main(int argc, char* argv[])
{
	vector<int> vector_created_on_stack ;
	
	for ( int i = 0 ; i < 10 ; i++ )
		vector_created_on_stack.push_back(i);


	ClassVectorPassing new_class( vector_created_on_stack);

	new_class.printVector();
	
	return 0;
}

you can use it's copy constructor even.This will copy your passing
vector fully to vector inside your class.

Yeh performance and there is a data duplication. However this is
the way if you want's to copy .


always use the copy constructor to copy.

0

Thanks a lot to all of you..I've sold my problem:))

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.