How to use std::find/std::find_if with a vector of custom class objects? - stl

I have a class representing a user called Nick and I want to use std::find_if on it, where I want to find if the userlist vector has an object included with the same username I pass in. I did a few attempts by trying to create a new Nick object for the username I want to test and overloading the == operator and then trying to use find/find_if on the object:
std::vector<Nick> userlist;
std::string username = "Nicholas";
if (std::find(userlist.begin(), userlist.end(), new Nick(username, false)) != userlist.end())) {
std::cout << "found";
}
I have overloaded the == operator so comparing Nick == Nick2 should work, but the function returns error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Nick' (or there is no acceptable conversion).
Here is my Nick class for reference:
class Nick {
private:
Nick() {
username = interest = email = "";
is_op = false;
};
public:
std::string username;
std::string interest;
std::string email;
bool is_op;
Nick(std::string d_username, std::string d_interest, std::string d_email, bool d_is_op) {
Nick();
username = d_username;
interest = d_interest;
email = d_email;
is_op = d_is_op;
};
Nick(std::string d_username, bool d_is_op) {
Nick();
username = d_username;
is_op = d_is_op;
};
friend bool operator== (Nick &n1, Nick &n2) {
return (n1.username == n2.username);
};
friend bool operator!= (Nick &n1, Nick &n2) {
return !(n1 == n2);
};
};

If you are using C++0X you can use a simple lambda expression
std::string username = "Nicholas";
std::find_if(userlist.begin(), userlist.end(), [username](Nick const& n){
return n.username == username;
})

You have to define operator== with two Objects outside your class, as a tool function, not a member.
Then to make it friend just put the declaration of the function inside the class.
try something like this:
class Nick {
public:
friend bool operator== ( const Nick &n1, const Nick &n2);
};
bool operator== ( const Nick &n1, const Nick &n2)
{
return n1.username == n2.username;
}
Also your find should look like this:
std::find(userlist.begin(), userlist.end(), Nick(username, false) );
No need of "new".

I know that you wanted to overload the == operator, but the same thing can easily be done with a predicate:
struct UsernameIs {
UsernameIs( string s ) : toFind(s) { }
bool operator() (const Nick &n)
{ return n.username == toFind; }
string toFind;
};
int main()
{
vector<Nick> vn(10);
string nameToFind = "something";
find_if(vn.begin(), vn.end(), UsernameIs(nameToFind));
}
Note that in C++0x, you can do the same thing with a lambda expression much more concisely.

You are passing a pointer to the find function. Drop the new:
std::find(userlist.begin(), userlist.end(), Nick(username, false))
Also, your operators should accept their arguments by const reference, they don't modify them.
bool operator== (const Nick &n1, const Nick &n2)

I am noticing you are trying to call one constructor from another in this manner:
Nick(std::string d_username, bool d_is_op) {
Nick();
...
Well, sorry, but this doesn't work. The line Nick() just creates a temporary and doesn't affect this. Constructor forwarding is only possible in C++0x (the upcoming standard)
As to your problem - this question asked a couple of days ago about binary_search covers the same grounds. The top answer is just awesome.
Mystical restriction on std::binary_search
HTH.
P.S. Ideally this should have been a comment, but it's just too verbose

You can use boost::bind
std::find_if( userlist.begin(), userlist.end(),
boost::bind( & Nick::isFound,
_1 ) );
just implement bool Nick::isFound()
You can also pass the criteria
std::find_if( userlist.begin(), userlist.end(),
boost::bind( & Nick::compare,
_1,
nick ) );
implement
bool Nick::compare( const Nick & nick )
{
return this->username == nick.username;
}

This works for me:
Nick.h
#include <string>
class Nick {
private:
Nick() {
username = interest = email = "";
is_op = false;
};
public:
std::string username;
std::string interest;
std::string email;
bool is_op;
Nick(std::string d_username, std::string d_interest, std::string d_email, bool d_is_op) {
Nick();
username = d_username;
interest = d_interest;
email = d_email;
is_op = d_is_op;
};
Nick(std::string d_username, bool d_is_op) {
Nick();
username = d_username;
is_op = d_is_op;
};
bool operator==(const Nick& refNick) const
{
if (username != refNick.username)
return false;
if (interest != refNick.interest)
return false;
if (email != refNick.email)
return false;
if (is_op != refNick.is_op)
return false;
return true;
}
bool operator!=(const Nick& refNick) const
{
if (username == refNick.username)
return true;
if (interest == refNick.interest)
return true;
if (email == refNick.email)
return true;
if (is_op == refNick.is_op)
return true;
return false;
}
};
main.cpp
#include <iostream>
#include <string>
#include <vector>
#include "Nick.h"
int main()
{
std::vector<Nick> userlist;
std::string username = "Nicholas";
Nick Nicholas(username, false);
Nick John("John", true);
userlist.push_back(Nicholas);
std::vector<Nick>::iterator it;
it = std::find(userlist.begin(), userlist.end(), Nick("Nicholas", false));
if(it != userlist.end())
std::cout << "\n" << Nicholas.username << " was found.";
else
std::cout << "\n" << Nicholas.username << " was not found.";
it = std::find(userlist.begin(), userlist.end(), John);
if (it != userlist.end())
std::cout << "\n" << John.username << " was found.";
else
std::cout << "\n" << John.username << " was not found.";
}
Result
Nicholas was found.
John was not found.

Related

Hi everyone. I need a little help on how to do this code. I have a main that I'm not allowed to change, but I have to make it work

this is how my main looks
b = 3;
cout << "Affectation OK\n";
const Uint c = 13;
cout << uint64_t(c) << " = 13: explicit cast to uint64_t\n";
this is how my class looks
class Uint {
private:
string nb;
public:
Uint();
Uint(size_t a);//Overload constructeur
operator uint64_t() {
return static_cast<uint64_t>(123456789UL);
}
};//LABO21_UINT_H
#endif
i tried so many ways to do that, but no succes. If anyone could help me, on how to use the operators on this , it would be a greatp help
The ctor:
Uint(size_t a){
//convert a to string
nb=...
}
If you can't change the main, you should add a friend function:
friend operator uint64_t(Uint c) {
return static_cast<uint64_t>(c.nb);
}
friend function give you the option to access the private fields of class eventhough it's not a member function.

boost shared_memory_object use of deleted function

use of deleted function in class operator=
old version worked with old compiler but not with new versions
I need this "operator=" overloading for container operation.
#include <boost/interprocess/ipc/message_queue.hpp>
#include <iostream>
using namespace boost::interprocess;
class X {
public:
size_t m_len;
shared_memory_object m_shm;
const char* m_ptr;
X():
m_len(0),
m_shm(shared_memory_object()),
m_ptr(nullptr){}
X(size_t t, const char* n):
m_len(t),
m_shm(shared_memory_object()),
m_ptr(nullptr){
shared_memory_object::remove(n);
m_shm = shared_memory_object(open_or_create,n, read_write);
m_shm.truncate (m_len);
mapped_region region(m_shm, read_write);
m_ptr = static_cast<char*>(region.get_address());
}
X(const X&& x){
m_len = x.m_len;
m_shm = x.m_shm; //error use deleted function
m_ptr = x.m_ptr;
}
virtual ~X(){}
X& operator = (const X&& a) {
if(&a == this) return *this;
m_len = a.m_len;
m_ptr = a.m_ptr;
m_shm = a.m_shm; //error use deleted function
return (*this);
}
const char* get_name(){
return m_shm.get_name();
}
};
int main ()
{
X a = X(22, "test");
X b = a; //Error
return 0;
};
The above class will be used in std::vector and operator= is needed.
boost shared_memory_object has member:
shared_memory_object(shared_memory_object &&);
shared_memory_object& operator=(shared_memory_object &&);
Move operations can only work on non-const objects. Why? Because it steals resources from the source instance, then it must be non-const to be modifiable. So your move operations should take non-const objects:
now should be
----------------------------------------------------------
X(const X&& x) ==> X(X&& x)
X& operator = (const X&& a) { ==> X& operator = (X&& a) {
Named variable is treated as L-value, it implies copy operations are called. You need to use std::move to cast source to R-value reference, then move operations can be called:
m_shm = std::move(x.m_shm); // in move ctor
m_shm = std::move(a.m_shm); // in move assignment operator
and finally to call move ctor:
X b = std::move(a);

Attempt at STL Container

I am attempting to make a version of std::set using a linked list. I think I have implemented it mostly correctly but I am getting a compile error that I cannot decipher. I would appreciate anyone spotting the error in my code, and or explaining how I would go about tracking down an error like this. Meaning an error that goes far into stl functions.
#include <iterator>
#include <cstddef>
template <typename Type>
struct ListNode{
Type info;
ListNode<Type> * next;
ListNode(Type newInfo, ListNode<Type> * newNext) : info(newInfo), next(newNext){
}
ListNode(ListNode<Type>& L): info(L.info), next(L.next){
}
ListNode<Type>& operator=(ListNode<Type>& L){
info = L->info;
next = L->next;
return this;
}
};
template <typename Type>
class SetList{
ListNode<Type> * head;
ListNode<Type> * tail;
public:
typedef ListNode<Type> value_type;
SetList() : head(nullptr), tail(nullptr){
}
SetList(SetList & s){
}
~SetList(){
//ListNode<Type> * cur = head;
//ListNode<Type> * next = cur;
//while(cur){
// next = cur->next;
// delete cur;
// cur = next;
// }
}
struct iterator{
//traits
typedef std::forward_iterator_tag iterator_category;
typedef iterator self_type;
typedef Type value_type;
typedef Type& reference;
typedef Type* pointer;
typedef ptrdiff_t difference_type;
private:
//rename to ihead
ListNode<Type>* ibuf;
public:
iterator(ListNode<value_type>* node) : ibuf(node){}
self_type& operator++(){ibuf = ibuf->next; return *this;}
self_type operator++(int postfix){
self_type cpy = *this;
ibuf = ibuf->next;
return cpy;
}
reference operator*(){return ibuf->info;}
pointer operator->(){return &ibuf->info;}
self_type operator=(const iterator& it){insert(*it);}
bool operator==(const self_type& rhs) const {return ibuf->info == rhs.ibuf->info;}
bool operator !=(const self_type& rhs) const {return ibuf->info != rhs.ibuf->info;}
};
iterator begin(){ return iterator(head);}
iterator end() { return iterator(nullptr);}
// const_iterator begin() { return const_iterator(head);}
// const_iterator end() { return const_iterator(tail);}
Type operator[](int index){
iterator cur(head);
for(int i = 0; i < index; ++i,++cur){
}
return *cur;
}
SetList<Type>& operator=(const SetList<Type>& s){
head = s.head;
tail = s.tail;
return this;
}
iterator find(Type toFind){
ListNode<Type> * cur = head;
while(cur){
if(cur->info == toFind)
return iterator(cur);
}
return this->end();
}
void insert(Type toInsert){
ListNode<Type>* cur = nullptr;
if(head){
cur = new ListNode<Type>(toInsert, head);
head = cur;
}else{
cur = new ListNode<Type>(toInsert, nullptr);
head = cur;
}
}
};
I am calling elsewhere copy on my set, my copy call works with std::set but not my set.
The error I am getting is as follows.
Hope this isn't too much to ask. You don't even have to read my code, even just input on how to track down large errors like this would be much appreciated.
SetList<Type> should have Type as its value_type, not ListNode<Type>.

How can I convert the decimal representation of an IP address into binary?

Does anyone knows how to convert decimal notation of an IP address into binary form in Java? Please let me know...
An IP address written as a.b.c.d can be converted to a 32-bit integer value
using shift and bit-wise inclusive OR operators as,
(a << 24) | (b << 16) | (c << 8) | d
To be safe, each of a,b,c,d has valid range 0-255 -- you can check that in your conversion.
You can further validate the IP address using this regex example.
You can use the java.net.InetAddress class. Two methods you should look at are getByName and getAddress. Here is a simple code example
import java.net.InetAddress;
import java.net.UnknownHostException;
/* ... */
String ip = "192.168.1.1";
InetAddress address = null;
try {
address = InetAddress.getByName(ip);
} catch (UnknownHostException e) {
//Your String wasn't a valid IP Address or host name
}
byte [] binaryIP = address.getAddress();
Gathering your suggestions and some other sources, I found usefull to convert an InetAdress to an array of bit, as well as BitSet, which can help to compute and(), or(), xor() out of your binary representation.
Following sample shows how to convert ip to binary and binary to ip.
Enjoy!
public class IpConverter {
public static void main(String[] args) {
String source = "192.168.1.1";
InetAddress ip = null;
try {
ip = InetAddress.getByName(source);
} catch (UnknownHostException e) {
e.printStackTrace();
return;
}
System.out.println( "source : " + ip);
// To bit sequence ------------
byte[] binaryIP = ip.getAddress();
BitSet[] bitsets = new BitSet[binaryIP.length];
int k = 0;
System.out.print("to binary: ");
for (byte b : binaryIP) {
bitsets[k] = byteToBitSet(b);
System.out.print( toString( bitsets[k] ) + ".");
k++;
}
System.out.println();
// Back to InetAdress ---------
byte[] binaryIP2 = new byte[4];
k = 0;
for (BitSet b : bitsets) {
binaryIP2[k] = bitSetToByte(b);
k++;
}
InetAddress ip2 = null;
try {
ip2 = InetAddress.getByAddress(binaryIP2);
} catch (UnknownHostException e) {
e.printStackTrace();
return;
}
System.out.println( "flipped back to : " + ip2);
}
public static BitSet byteToBitSet(byte b) {
BitSet bits = new BitSet(8);
for (int i = 0; i < 8; i++) {
bits.set(i, ((b & (1 << i)) != 0) );
}
return bits;
}
public static byte bitSetToByte(BitSet bits) {
int value = 0;
for (int i = 0; i < 8; i++) {
if (bits.get(i) == true) {
value = value | (1 << i);
}
}
return (byte) value;
}
public static byte bitsToByte(boolean[] bits) {
int value = 0;
for (int i = 0; i < 8; i++) {
if (bits[i] == true) {
value = value | (1 << i);
}
}
return (byte) value;
}
public static boolean[] byteToBits(byte b) {
boolean[] bits = new boolean[8];
for (int i = 0; i < bits.length; i++) {
bits[i] = ((b & (1 << i)) != 0);
}
return bits;
}
public static String toString(BitSet bits){
String out = "";
for (int i = 0; i < 8; i++) {
out += bits.get(i)?"1":"0";
}
return out;
}
}
The open-source IPAddress Java library can do this for you. It can parse various IP address formats, including either IPv4 or IPv6, and has methods to produce various string formats, including one for binary. Disclaimer: I am the project manager of the IPAddress library.
This code will do it:
static void convert(String str) {
IPAddressString string = new IPAddressString(str);
IPAddress addr = string.getAddress();
System.out.println(addr + " in binary is " + addr.toBinaryString());
}
Example:
convert("1.2.3.4");
convert("a:b:c:d:e:f:a:b");
The output is:
1.2.3.4 in binary is 00000001000000100000001100000100
a:b:c:d:e:f:a:b in binary is 00000000000010100000000000001011000000000000110000000000000011010000000000001110000000000000111100000000000010100000000000001011

Custom STL Containers

I have written code that allows one to traverse mapped data in the order it was entered.
The solution I coded a couple of times was:
Given a keytype, K, and and data type, D,
std::map
std::vector
When one wanted to randomly find a data entry, use map.find(K). When one wanted to traverse the map in entry order, use std::vector::iterator (begin(), end()].
This was fine, but as an exercise, I wanted to write this 'OrderedMap' as an STL compliant container. I also have (stripped down to this discussion):
template <typename K, typename D>
class OrderedMapValue
{
private:
K first_ref;
std::map<K,size_t>& m;
std::vector<D>& v;
public:
const K& first
D& second
assignment operator=(const D& data)
{
std::map<K,size_t>::const_iterator iter = m.find(first_ref);
v[iter.second] = data; // error checking of iter stripped
}
};
Further assuming
template <typename K, typename D>
class OrderedMap
{
public:
typename OrderedMapValue<K,D>& OrderedMap<K,D>::operator[](const K&);
// snip...
};
class MyClass
{
public:
MyClass(std::string s) : _my_data(s) {}
private:
std::string _my_data;
};
The following code works:
OrderedMap<std::string,MyClass*> omap;
omap["MyKey"] = new MyClass("dummy");
However, this code does not:
OrderedMap::iterator iter = omap.find("MyKey");
MyClass * obj = iter->second;
delete obj;
iter->second = new MyClass("dummy");
Assuming I have done something
a) Structurally silly or
b) Unnecessarily complex, how should this be done?
I realize that I'm likely reinventing the wheel here, but again, this effort is mainly to increase my knowledge of STL containers, their design patterns and proper use.
Thanks in advance for any insights,
I don't have a compiler right now to test this, so there could be errors, but I think you want it more like:
template <typename K, typename D>
class OrderedMap
{
private:
std::map<K,size_t> &m;
std::vector<D> &v;
public:
typename pair<K,D> TYPE;
TYPE& operator[](const K &k)
{
return v[ m[ k ]];
}
TYPE& operator[](size_t idx)
{
return v[ idx ];
}
pair<iterator,bool> insert( const TYPE& pair )
{
map<K, size_t>::const_iterator iter;
iter = m.find( pair.first );
if( iter != m.end() )
return make_pair( v[ iter.second], false );
m.insert( make_pair( pair->first, v.size() ));
v.push_back( pair->second );
return make_pair( v.last() , inserted );
}
iterator &begin()
{
return v.begin();
}
// etc
};
In OrderedMapValue::operator=, you have:
std::map<K,size_t>::const_iterator iter = m.find(first_ref);
What is first_ref? The code doesn't reference it (no pun intended) elsewhere. It looks to me like it might be a vestige from an older implementation, replaced elsewhere by the public member
const K& first.
Could this be the problem?
EDIT from the comments: The code doesn't show that first_ref is initialized anywhere; so for all I can tell, the call to m.find(first_ref) is searching for an empty string, rather than the key for the OrderedMapValue.