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??

Why not just pass it to the object as a parameter?

or as a reference

>>or as a reference
Good Point.
Its mostly better to pass arguments by-reference-to-const rather than by-value.

// 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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.