-
January 18th, 2011, 11:56 PM
#1
std::vector class with functions?
I have a typedef std::vector that I'd like to expand to have a function that will seek members with a specific value in a variable. I currently have something like
Code:
class MyClass {
public:
int a;
};
typedef std::vector<MyClass> MyClassArr;
How could I have the MyClassArr type have additional functions?
Thank you in advance.
EDIT:
I believe I figured it out.
Code:
class MyClass {
public:
int a;
};
class MyClassArr:public std::vector<MyClass> {
void func();
};
Last edited by lawl_rock; January 19th, 2011 at 12:43 AM.
-
January 19th, 2011, 03:31 AM
#2
Re: std::vector class with functions?
Originally Posted by lawl_rock
I have a typedef std::vector that I'd like to expand to have a function that will seek members with a specific value in a variable.
That is what algorithms such as find_if() are supposed to do. You shouldn't derive from std::vector, as the vector class is not designed for the purpose of being derived from.
I believe I figured it out.
Code:
class MyClass {
public:
int a;
};
class MyClassArr:public std::vector<MyClass> {
void func();
};
This is no solution. The proper solution is to use find_if(), for_each(), etc.
Code:
#include <algorithm>
//...
std::vector<MyClass> m;
//...
bool FindMyValue(const MyClass& theClass)
{
if ( /* theClass matches my criteria */ )
return true;
return false;
}
//...
std::vector<MyClass>::iterator m_It = std::find_if(m.begin(), m.end(), FindMyValue);
if ( m_It != m.end() )
{
// found the value. m_It points to that value
}
else
{
// not found
}
That is one way to do this. Another way is to use function objects (functors) as the third argument to find_if.
Any code that derives from vector just to add a function is not recommended, and is absolutely not necessary for what you want to do. STL isn't just containers -- there are algorithms that provide the framework to work with the containers and iterators.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; January 19th, 2011 at 03:47 AM.
-
January 19th, 2011, 10:30 AM
#3
Re: std::vector class with functions?
Instead of deriving from std::vector you could have the std::vector a member of your class. Then you easily can offer a specialized interface to your container and nevertheless use all members and template functions for std::vector in your member functions.
-
January 19th, 2011, 03:44 PM
#4
Re: std::vector class with functions?
Alright thank you, I am looking into find_if. How could I use find_if to create a vector of MyClass that contains members (from an original vector) that meet a specific criteria? I.e., if I had original vector A containing members with a wide range of values for a variable, how could I make vector B copy all of those members whose variable is within a range of say, 3 to 5?
-
January 19th, 2011, 05:01 PM
#5
Re: std::vector class with functions?
For that I'd recommend remove_copy_if(). Some compiler support copy_if, which is really more appropriate, but some don't; remove_copy_if() is supported everywhere. All you have to do is reverse your condition (make the predicate return true if you do *not* want an item to be copied).
You'll need to use an output iterator; I recommend reading up on std::back_inserter which is fairly easy to use.
-
January 19th, 2011, 05:06 PM
#6
Re: std::vector class with functions?
Your class' constructor could take two iterators mimicking the way std::sort, std::unique or std::vector itself does it?
-
January 19th, 2011, 05:28 PM
#7
Re: std::vector class with functions?
I followed up on itsmeandnobodyelse and Lindley's advice and came up with the following code:
Code:
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
class MyClass {
public:
int a, b;
MyClass() {
a=b=0;
}
MyClass(int aa, int ab) {
a=aa;
b=ab;
}
};
typedef vector<MyClass> MyClassVector;
struct IsInRange:public std::unary_function<MyClass, bool> {
int lowR;
int highR;
IsInRange(int lowRange, int highRange) {
lowR=lowRange;
highR=highRange;
}
bool operator () (const MyClass i) const {
return ((i.a >= lowR)&&(i.a < highR));
}
};
class MyClassArray {
public:
MyClassVector v;
MyClassArray() {}
MyClassArray(vector<MyClass> vv) {v=vv;}
MyClassArray GetInRange(int lowRange, int highRange);
};
MyClassArray MyClassArray::GetInRange(int lowRange, int highRange) {
MyClassVector temp;
copy_if(v.begin(), v.end(), back_inserter(temp), IsInRange(1, 3));
MyClassArray re(temp);
return re;
}
int main() {
MyClassArray ma;
for (int i=0; i<5; i++) {
ma.v.push_back(MyClass(i, 0));
}
for (int i=0; i<ma.v.size(); i++) {
cout<<ma.v[i].a<<'\n';
}
cout<<'\n';
MyClassArray mb=ma.GetInRange(1, 3);
for (int i=0; i<mb.v.size(); i++) {
cout<<mb.v[i].a<<'\n';
}
cin.get();
}
It compiles and works as planned, but being self-taught, I'd like to know if there are any don'ts I'm violating here. Thank you all
-
January 19th, 2011, 05:31 PM
#8
Re: std::vector class with functions?
The only thing I see offhand is that in your predicate's operator(), you could be passing i by const reference rather than by value. This is usually more efficient.
-
January 19th, 2011, 05:49 PM
#9
Re: std::vector class with functions?
Like this?
Code:
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;
class MyClass {
public:
int a, b;
MyClass() {
a=b=0;
}
MyClass(int aa, int ab) {
a=aa;
b=ab;
}
};
typedef vector<MyClass*> MyClassVector;
struct IsInRange:public std::unary_function<MyClass, bool> {
int lowR;
int highR;
IsInRange(int lowRange, int highRange) {
lowR=lowRange;
highR=highRange;
}
bool operator () (const MyClass* i) const {
return ((i->a >= lowR)&&(i->a < highR));
}
};
class MyClassArray {
public:
MyClassVector v;
MyClassArray() {}
MyClassArray(vector<MyClass*> vv) {v=vv;}
MyClassArray GetInRange(int lowRange, int highRange);
};
MyClassArray MyClassArray::GetInRange(int lowRange, int highRange) {
MyClassVector temp;
copy_if(v.begin(), v.end(), back_inserter(temp), IsInRange(1, 3));
MyClassArray re(temp);
return re;
}
int main() {
MyClassArray ma;
for (int i=0; i<5; i++) {
ma.v.push_back(new MyClass(i, 0));
}
for (int i=0; i<ma.v.size(); i++) {
cout<<ma.v[i]->a<<'\n';
}
cout<<'\n';
MyClassArray mb=ma.GetInRange(1, 3);
for (int i=0; i<mb.v.size(); i++) {
cout<<mb.v[i]->a<<'\n';
}
cin.get();
}
-
January 19th, 2011, 05:51 PM
#10
Re: std::vector class with functions?
Pass by reference looks like this:
Code:
bool operator () (const MyClass& i) const
What you did above is to pass a pointer. That is a similar concept, but won't work in this case.
-
January 19th, 2011, 06:05 PM
#11
Re: std::vector class with functions?
I didn't realize it'd be such a simple change, thank you!
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
|