stdvector

Saving Crypto++ objects to std::vector

做~自己de王妃 提交于 2019-12-02 03:07:58
问题 I want to save Crypto++ keys to std::vector<uint8_t> . Unfortunately there is only CryptoPP::StringSink , that takes std::string reference but no CryptoPP::VectorSink that would take a reference to std::vector . Following code works fine std::string spki; CryptoPP::StringSink ss(spki); CryptoPP::RSA::PublicKey publicKey(...); publicKey.Save(ss); But I want this std::vector<uint8_t> spki; CryptoPP::VectorSink vs(spki); CryptoPP::RSA::PublicKey publicKey(...); publicKey.Save(vs); The problem

Sort by Even and Odd numbers

廉价感情. 提交于 2019-12-02 01:11:33
问题 I would like to know is it possible to sort number by even or odd using the std::sort function. I have the following codes but i am not sure how to implement in the std::sort inline bool isEven(const Point n) { return n.getX()%2==0; } Is this correct vector<Point> c; std::sort(c.begin(),c.end(),isEven) Please advice. 回答1: From what I understand of your question, you want to separate odd and even numbers. If that's the case, std::partition will do just that. If you want to sort by ascending

Saving Crypto++ objects to std::vector

我的梦境 提交于 2019-12-01 23:17:31
I want to save Crypto++ keys to std::vector<uint8_t> . Unfortunately there is only CryptoPP::StringSink , that takes std::string reference but no CryptoPP::VectorSink that would take a reference to std::vector . Following code works fine std::string spki; CryptoPP::StringSink ss(spki); CryptoPP::RSA::PublicKey publicKey(...); publicKey.Save(ss); But I want this std::vector<uint8_t> spki; CryptoPP::VectorSink vs(spki); CryptoPP::RSA::PublicKey publicKey(...); publicKey.Save(vs); The problem VectorSink can not be created just by using a typedef because of traits_type::char_type inside

Should I iterate a vector by iterator or by access operator?

有些话、适合烂在心里 提交于 2019-12-01 19:17:19
I have a vector declared as std::vector<int> MyVector; MyVector.push_back(5); MyVector.push_back(6); MyVector.push_back(7); How do should I use it in a for loop? By iterating it with an iterator? for (std::vector<int>::iterator it=MyVector.begin(); it!=MyVector.end(); ++it) { std::cout << "Vector element (*it): " << *it << std::endl; } Or by its access iterator? for (std::vector<int>::size_type i=0; i<MyVector.size(); i++) { std::cout << "Vector element (i) : " << MyVector.at(i) << std::endl; } In examples I found on internet both of them are used. Is one of them superior to the other under

C++: std::vector [] operator

匆匆过客 提交于 2019-12-01 18:06:16
Why std::vector has 2 operators [] realization ? reference operator[]( size_type pos ); const_reference operator[]( size_type pos ) const; One for non-const vector object, and the other for const vector object. void f(std::vector<int> & v1, std::vector<int> const & v2) { //v1 is non-const vector //v2 is const vector auto & x1 = v1[0]; //invokes the non-const version auto & x2 = v2[0]; //invokes the const version v1[0] = 10; //okay : non-const version can modify the object v2[0] = 10; //compilation error : const version cannot modify x1 = 10; //okay : x1 is inferred to be `int&` x2 = 10; /

Should I iterate a vector by iterator or by access operator?

孤街醉人 提交于 2019-12-01 17:54:23
问题 I have a vector declared as std::vector<int> MyVector; MyVector.push_back(5); MyVector.push_back(6); MyVector.push_back(7); How do should I use it in a for loop? By iterating it with an iterator? for (std::vector<int>::iterator it=MyVector.begin(); it!=MyVector.end(); ++it) { std::cout << "Vector element (*it): " << *it << std::endl; } Or by its access iterator? for (std::vector<int>::size_type i=0; i<MyVector.size(); i++) { std::cout << "Vector element (i) : " << MyVector.at(i) << std::endl;

Is there a more efficient way to set a std::vector from a stream?

孤者浪人 提交于 2019-12-01 17:24:15
Presently, I set the value of a std::vector<char> from an std::ostringstream as follows: void foo(std::vector<char> &data, std::stringstream &stream) { data = std::vector<char>(stream.str().begin(), stream.str().end()); } I'm wondering if there is a more efficient way to do this with STL in C++ or whether the method I give here is considered appropriate? Would I be better off using std::stringstream instead? Your method invokes undefined behaviour . stream.str() returns a string by-value , aka a temporary string. You take the begin iterator of one temporary and the end iterator of the other,

C++: std::vector [] operator

旧时模样 提交于 2019-12-01 17:04:29
问题 Why std::vector has 2 operators [] realization ? reference operator[]( size_type pos ); const_reference operator[]( size_type pos ) const; 回答1: One for non-const vector object, and the other for const vector object. void f(std::vector<int> & v1, std::vector<int> const & v2) { //v1 is non-const vector //v2 is const vector auto & x1 = v1[0]; //invokes the non-const version auto & x2 = v2[0]; //invokes the const version v1[0] = 10; //okay : non-const version can modify the object v2[0] = 10; /

std::vector::erase(item) needs assignment operator to be defined for item?

丶灬走出姿态 提交于 2019-12-01 16:40:06
问题 I have a class C which does not define operator= . I have am trying to use a vector like so: std::vector<std::pair<C,D>> vec; . Now, my problem is that I am not able to erase the pair after I am done with it. The compiler complains of missing operator= for C . Can I not have a vector of a class which does not have this operator? How do I get around this? I cannot add assignment to C . This is the error I get: error C2582: 'operator =' function is unavailable in 'C' C:\...\include\utility 196

std::vector::erase(item) needs assignment operator to be defined for item?

半世苍凉 提交于 2019-12-01 16:32:25
I have a class C which does not define operator= . I have am trying to use a vector like so: std::vector<std::pair<C,D>> vec; . Now, my problem is that I am not able to erase the pair after I am done with it. The compiler complains of missing operator= for C . Can I not have a vector of a class which does not have this operator? How do I get around this? I cannot add assignment to C . This is the error I get: error C2582: 'operator =' function is unavailable in 'C' C:\...\include\utility 196 1 my-lib This is my code: void Remove(const C& c) { auto i = cs_.begin(); while( i != cs_.end()) { if