Click to See Complete Forum and Search --> : vector understanding
Douglas999
March 10th, 2006, 06:40 PM
This is abou vector
#include<vector>
void vect(std::vector<int>b, int i){
b.remove(i);
}
void main(){
std::vector<int> h;
vect(h,1);
double x=0, y=0 ;
h.push_back(x);
h.push_back(y);
return 0;
}
But it gave me a lot of error I don't understand :(. This is part of a a smell project at school.
If u give more more advice, i promise you i will be back to say more thanks.
Thanks for your inputs, you are helpfull, very full.
Douglas999
Philip Nicoletti
March 10th, 2006, 07:24 PM
1) You need to pass the vector by reference, not by value.
2) I assume you are gtrying to remove the i-th element from the
vector. The member function is called erase(), not remove(). Also
it takes an iterator, not an index.
void vect(std::vector<int> & b, int i){
//assumes that there are at least (i+1) elements in the vector
b.erase(b.begin()+i);
}
Code_Nerd
March 10th, 2006, 07:37 PM
std::vector<int> h;
vect(h,1);
double x=0, y=0 ;
h.push_back(x);
h.push_back(y);
You are also trying to push doubles onto a int vector..
SuperKoko
March 11th, 2006, 02:38 AM
void main(){
The correct function prototype is int main.
std::vector<int> h;
vect(h,1);
What is it supposed to do?
Remove the second (vector begin at 0) element of an empty vector.
Perhaps, you should move that operation after the second push_back method call.
#include <vector>
void vect(std::vector<int> b, std::vector<int>::size_type i){ // std::vector<int>::size_type is an unsigned integer whose size is adequate for vector indexing
b.erase(b.begin()+i);
}
int main(){
std::vector<int> h;
int x=0, y=0 ;
h.push_back(x);
h.push_back(y);
vect(h,1); // this operation and the previous mutually anihilate themselves.
return 0;
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.