0

I'm using this function to find value in vector.

void program_data::find_curlAccountdata(int world, std::wstring login, std::wstring pass, curl_accounts & tmp){
    std::unordered_map <int, std::vector<curl_accounts> > &t = curl_accountsdata; //world
    std::vector<curl_accounts> &b = t[world];
    std::vector<curl_accounts>::iterator & it = std::find_if(b.begin(), b.end(), [login, pass](curl_accounts& s) { return (s.name == login && s.pass == pass); });
    if (it != b.end()){
        tmp = *it;
    }
}

And curl_accounts is struct.

Now when I edit some value in the struct in one place, and try to check it in second place it not the same.

So could you fix my function?

EDIT::

curl_accounts tmp,tmp2;
program_data::getInstance().find_curlAccountdata(server, login, pass, tmp);
tmp.logged = true;
std::cout << "TMP LOGGED: "<< tmp.logged<<std::endl; // return true
program_data::getInstance().find_curlAccountdata(server, login, pass, tmp2);
std::cout << "TMP 2 LOGGED: " << tmp2.logged << std::endl; // return false
std::cout << "sth";
Niall
  • 30,036
  • 10
  • 99
  • 142
Thomas Banderas
  • 1,681
  • 1
  • 21
  • 43
  • 1
    The references to items stored in the vector may invalidate as soon the original vector is changed. – πάντα ῥεῖ Aug 25 '14 at 10:11
  • So maybe i should use pointer? – Thomas Banderas Aug 25 '14 at 10:13
  • Pointers and references are the same thing (under the hood) and therefore have the same invalidation rules. If (and ONLY if) you are sure to not do any invalidating operations, you are fine with either. – Seg Fault Aug 25 '14 at 10:16
  • This doesn't "return" a reference at all. It *assigns* through the reference provided as `tmp`. Once initialized (which is required) you cannot change a reference, only what it *references*. (You can, of course, create a "dangling" reference if you set yourself to it). – WhozCraig Aug 25 '14 at 10:16
  • 3
    You can't create a non-`const` reference to an rvalue (`it`), this code shouldn't even compile. – user657267 Aug 25 '14 at 10:17
  • @user657267 doesn't MS's "compiler" support that nonsense? – WhozCraig Aug 25 '14 at 10:21
  • @user657267 He's probably using MSVC. tl;dr: it's an error, but MSVC allows it to be compiled without a diagnostic. –  Aug 25 '14 at 10:21
  • @WhozCraig @remyabel I had no idea, what an interesting bug `^H^H^H` feature. – user657267 Aug 25 '14 at 10:24
  • Yes i'm using MSVC, so i have to rebuild my function? – Thomas Banderas Aug 25 '14 at 10:25
  • 1
    @ThomasBanderas I think time [with this question](http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c?rq=1) may help. No, you don't have to change too much, but `it` shouldn't be a reference. It should be just an iterator. – WhozCraig Aug 25 '14 at 10:32

1 Answers1

-1

You need a reference to a pointer. Try changing your signature to use curl_accounts *& tmp, instead of curl_accounts &tmp.

Nick Ligerakis
  • 301
  • 4
  • 7
  • You can't have a pointer to a reference. You *can* have a reference to a pointer (which your syntax shows, but your opening sentence contradicts). – WhozCraig Aug 25 '14 at 10:17
  • you're right, edited to reflect my intent. the signature is still right, while the comment's wrong. – Nick Ligerakis Aug 25 '14 at 10:30