How to insert value in Vector (Dinamic Array) using For?

Hello, i want to make a function to be able to Insert a value in an already set Vector or dinamic array, using FOR, ive done this much so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "encabezado.h"


Contenedor::Contenedor(){
	inicializar();
	Cantidad = 0; 
	Tamano = 10; //Vectors lenght.

}
Contenedor::~Contenedor(){

}


int Contenedor::getCantidad(){
	return Cantidad;
}
int Contenedor::getTamano(){
	return Tamano;
}


void Contenedor::inicializar(){
	cout << "--------Inicializando--------" << endl;
	for (int i = 0; i<Tamano; i++)
		Vector[i] = 0;

}
bool Contenedor::agregaElemento(int ele){
	if (Cantidad == Tamano)
		return false;
	else{
		Vector[Cantidad] = ele;
		Cantidad++;
		return true;
	}
}
void Contenedor::imprimeContenedor(){
	int x = 0;
	for (int i = 0; i < Cantidad; i++){

		cout << x++ << ") " << Vector[i] << endl;
	}
	cout << endl <<  "------------------------------" << endl;
}
void Contenedor::invertir(){
	cout << "------ Invirtiendo ------" << endl; 
	int temporal;
	for (int i = 0; i < Cantidad / 2; ++i){
		temporal = Vector[i];
		Vector[i] = Vector[Cantidad - i - 1];
		Vector[Cantidad - i - 1] = temporal;
	}	 // codigo escrito con ayuda de: "Softrix", 
		// miembro de forum oficial de c++ : mas info: http://www.cplusplus.com/forum/beginner/132247/ ...
}

bool Contenedor::insertarPos(int pos, int ele){
	if (pos>Cantidad)
	{
		cout << "No hay espacio para insertar!" << endl;
		return false;
	}
	else {
		//Scan to da right.
		int y = 1;
		cout << "Insertando valor: " << ele << ", en la posicion: " << (pos + 1) << "." << endl;
		int temp = Vector [pos];
		int temp2 = Vector[pos + y];
		
		
		for (int i = 0; i < Vector[pos]; i++)
			
			Vector[pos] = ele;
			Vector[(pos + y)] = temp;

			y++;
			Cantidad++;
			
			Vector[pos + y] = temp;
			Vector[pos + y] = temp2;
			Vector[pos + (y + 2)];
			y++;
			
					
		}
}


At this point, you can see when compiled that, the last value isnt showing up. Also, i find my code extremely un efficient so i would like to know if it would work if i keep entering new values. Can you tell. Thanks a lot for your time, friends.

Additional files:
Class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

class Contenedor{
private:
	int Vector[10];
	int Cantidad;
	int Tamano;

public:
	//constructor 
	 Contenedor();
	~Contenedor();

	//gets
	int getCantidad();
	int getTamano();

	//metodos de calculo 
	void inicializar();
	bool agregaElemento(int ele);
	void imprimeContenedor();
	void invertir();
	int cantidadPares();
	int cantidadPrimos(double n);
	int eleMayor();
	int sumaTotal();
	bool insertarPos(int pos, int ele);
	bool eliminarPos(int pos);
	bool eliminarEleRep(int ele);
	void ordenarElementos(); //de men a may
	bool intercambioPos(int pos1, int pos2); //ejercicios de la pag 76 y 77




};


Source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int main(){
	cout << "------------------------------------------------------------------" << endl;
	//creacion de un contenedor 
	Contenedor C1;
	//agragando elementos al contenedo
	cout << "Elementos: " << endl;

	C1.agregaElemento(10);
	C1.agregaElemento(20);
	C1.agregaElemento(40);
	C1.agregaElemento(7);
	C1.agregaElemento(9);
	C1.agregaElemento(5);
	cout << endl << endl;
	//imprimiendo el contenedorC1
	C1.imprimeContenedor();
	cout << endl << endl;
	C1.cantidadPares();
	C1.invertir();
	C1.imprimeContenedor();
	//	C1.cantidadPrimos();
	//inserta en posicion
	C1.insertarPos(3,5);
	//imprimiendo el contenedorC1
	C1.imprimeContenedor();

	cout << "------------------------------------------------------------------" << endl;
	system("pause");
	return 0;
}



Note that vector has insert() member function: http://www.cplusplus.com/reference/vector/vector/insert/
So your code can be rewritten as:
1
2
3
4
5
6
7
8
bool Contenedor::insertarPos(int pos, int ele){
	if (pos>Cantidad) {
		cout << "No hay espacio para insertar!" << endl;
		return false;
	}
	Vector.insert(vector.begin() + pos, ele);
	return true;
}
Last edited on
Yes, MiiNiPaa, thanks a lot, im aware of that but theyre asking to use For !
for(;false;); //Hey, I have used "for"   

Serious version:
1
2
3
4
5
6
7
8
9
10
11
bool Contenedor::insertarPos(int pos, int ele){
	if (pos>Cantidad) {
		cout << "No hay espacio para insertar!" << endl;
		return false;
	}
	Vector.push_back(ele);
	int last = Vector.size() - 1;
	for(int i = pos, i < last; ++i)
		std::swap(Vector[i], Vector[last])
	return true;
}
Topic archived. No new replies allowed.