|
-
March 10th, 2006, 07:40 PM
#1
vector understanding
This is abou vector
PHP Code:
#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
-
March 10th, 2006, 08:24 PM
#2
Re: vector understanding
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.
Code:
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);
}
-
March 10th, 2006, 08:37 PM
#3
Re: vector understanding
Code:
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..
-
March 11th, 2006, 03:38 AM
#4
Re: vector understanding
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.
Code:
#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;
}
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|