Thread View: pl.comp.lang.c
3 messages
3 total messages
Started by Borneq
Mon, 13 Jun 2016 10:08
Szukanie w unordered_map potrzebuje operatora == a on jest
Author: Borneq
Date: Mon, 13 Jun 2016 10:08
Date: Mon, 13 Jun 2016 10:08
120 lines
3140 bytes
3140 bytes
Jest:
using namespace std;
struct inputTx_s
{
uint8_t hash[32];
uint32_t index;
int cmp(const inputTx_s& rhs)
{
int res = 0;
bool isNonZero = false;
for (int i = 0; i < 32; i++)
if (hash[i] < rhs.hash[i])
{
res = -1;
break;
}
else if (hash[i] > rhs.hash[i])
{
res = 1;
break;
}
else if (hash[i] != 0)
isNonZero = true;
if (isNonZero)
{
if (index < rhs.index)
res = -1;
else if (index > rhs.index)
res = 1;
}
return res;
}
bool operator < (const inputTx_s& rhs)
{
return cmp(rhs)<0;
}
bool operator <= (const inputTx_s& rhs)
{
return cmp(rhs) <= 0;
}
bool operator == (const inputTx_s& rhs)
{
return cmp(rhs) == 0;
}
bool operator != (const inputTx_s& rhs)
{
return cmp(rhs) != 0;
}
bool operator >= (const inputTx_s& rhs)
{
return cmp(rhs) >= 0;
}
bool operator > (const inputTx_s& rhs)
{
return cmp(rhs) > 0;
}
};
struct outputTx_s
{
char address[36];
uint64_t amount;
outputTx_s() {};
outputTx_s(string addrStr)
{
strncpyc(address, addrStr.c_str(), sizeof(address));
}
};
struct hash_inputTx {
//http://isthe.com/chongo/tech/comp/fnv/
static size_t FNV_1(const uint8_t *bytes, size_t count)
{
size_t hash, FNV_prime;
if (sizeof(size_t) > 4)
{
hash = 14695981039346656037;
FNV_prime = 1099511628211;
}
else
{
hash = 2166136261;
FNV_prime = 16777619;
}
if (count > 0)
for (size_t i = 0; i < count; i++)
{
hash *= FNV_prime;
hash ^= bytes[i];
}
return hash;
}
size_t operator()(const inputTx_s &tx) const {
return FNV_1(tx.hash, sizeof(tx.hash)) ^ FNV_1((uint8_t*)&tx.index,
sizeof(tx.index));
}
};
=================================
Mam unordered_map<inputTx_s, outputTx_s, hash_inputTx> map;
Błąd gdy unordered_map<inputTx_s, outputTx_s,
hash_inputTx>::const_iterator iter = map.find(input);
Severity Code
Description Project File
Line Suppression State
Error C2678
binary '==': no operator found which takes a left-hand operand of type
'const inputTx_s' (or there is no acceptable conversion)
BlockNew C:\Program Files (x86)\Microsoft Visual Studio
14.0\VC\include\xstddef
225
Re: Szukanie w unordered_map potrzebuje operatora == a on jest
Author: Borneq
Date: Mon, 13 Jun 2016 10:41
Date: Mon, 13 Jun 2016 10:41
7 lines
1070 bytes
1070 bytes
W dniu 13.06.2016 o 12:08, Borneq pisze:
> bool operator == (const inputTx_s& rhs)
> {
> return cmp(rhs) == 0;
> }
Już wiem! operator jest , tylko musi być const
Re: Szukanie w unordered_map potrzebuje operatora == a on jest
Author: bartekltg
Date: Mon, 13 Jun 2016 17:38
Date: Mon, 13 Jun 2016 17:38
39 lines
2082 bytes
2082 bytes
W dniu poniedziałek, 13 czerwca 2016 12:08:42 UTC+2 użytkownik Borneq napisał:
> Jest:
> using namespace std;
>
> struct inputTx_s
> {
> uint8_t hash[32];
> uint32_t index;
>
> int cmp(const inputTx_s& rhs)
int cmp(const inputTx_s& rhs) const
> {
> int res = 0;
> bool isNonZero = false;
> for (int i = 0; i < 32; i++)
....
> return res;
> }
>
> bool operator < (const inputTx_s& rhs)
bool operator < (const inputTx_s& rhs) const
> {
> return cmp(rhs)<0;
> }
>
> bool operator == (const inputTx_s& rhs)
bool operator == (const inputTx_s& rhs) const
I tak dalej.
pzdr
bartekltg
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads