stl

Matrix Circular Shift

一笑奈何 提交于 2019-12-24 00:33:22
问题 Does anyone know an efficient way to right circular-shift a matrix? Btw, the matrix is binary but a method to solve a non-binary matrix is also fine. Right now, I'm thinking of implementing a circular array for the rows of my matrix and updating each row whenever a shift operation is required. Another method, I was considering was implementing a vector of pointers to columns (of the matrix) represented by vectors and swapping them around when a shift operation occurs. E.g. 1 2 3 4 5 6 7 8 9

STL: Initializing a container with an unconstructed stateful comparator

荒凉一梦 提交于 2019-12-24 00:08:55
问题 This has been running through my mind as a possible solution to an issue, however as it is a fairly obvious technical violation of something in C++, I wanted to know how likely to it is to fail, whether there is another fairly obvious approach, etc. I'm hoping this doesn't get into a flamewar about undefined behavior, but considering the topic I do expect a little bit. This is not the code I'm writing, I'm hoping it's not too simplified to not describe what I am attempting to do. class Code {

Overloading C++ STL methods

人盡茶涼 提交于 2019-12-24 00:05:04
问题 How can I overload the STL implementation for methods like find, erase and insert to take varying parameters? I tried to look up the overloading of STL methods but couldn't find any help. 回答1: You can't overload the methods of a class without editing the code of that class. Write your own free functions that act as helpers; they would take the relevant container class as the first parameter. You can inherit from a class and add methods that way, but the std container classes are not designed

Possible memory leak with malloc, struct, std::string, and free

邮差的信 提交于 2019-12-23 23:53:28
问题 I've a situation like the following, and I'm not sure whether or not the std::string elements of the struct leak memory or if this is ok. Is the memory allocated by those two std::strings deleted when free(v) is called? struct MyData { std::string s1; std::string s2; }; void* v = malloc(sizeof(MyData)); ... MyData* d = static_cast<MyData*>(v); d->s1 = "asdf"; d->s2 = "1234"; ... free(v); Leak or not? I'm using the void-pointer because I have another superior struct, which consists of an enum

std::remove_if not working properly [duplicate]

那年仲夏 提交于 2019-12-23 23:25:07
问题 This question already has answers here : Erasing elements from a vector (5 answers) Closed 5 years ago . Here my code. I want remove from vector all elements with successfully called method 'release'. bool foo::release() { return true; } // ... vector<foo> vec; // ... remove_if(vec.begin(), vec.end(), [](foo & f) { return f.release() == true; }); // ... But remove_if not deleting all elements from vector vec . How remove_if works? 回答1: std::remove_if re-arranges the elements of the vector

nested std::map using pointers

 ̄綄美尐妖づ 提交于 2019-12-23 23:16:50
问题 I am using a map inside a map and want to access a specific member in the second map. std::map<int, std::map<DWORD,IDLL::CClass*>*> MyMap 回答1: Try auto outerIt = MyMap.find(someInt); if(outerIt != MyMap.end()) { auto innerIt = (*outerIt)->find(someDWord); if(innerIt != (*outerIt)->end()) { auto yourElement = *innerIt; } } 回答2: If you are sure the keys exist, you could also try: IDLL::CClass* x = (*MyMap[key1])[key2]; 回答3: You can use std::map::find in two steps: first to find the value

Is thare in STL or BOOST map like container with find and pop operation?

和自甴很熟 提交于 2019-12-23 22:46:54
问题 I want my map to be searchable and I want to be capable to kick out from it elements that were inserted into it longest time ago (with api like map.remove(map.get_iterator_to_oldest_inserted_element()) ) like mix of queqe and map.. Is there any such container in STL or Boost? 回答1: You can use boost::multi_index using the ordered_unique and sequence indices, as in this example. #include <iostream> #include <boost/multi_index_container.hpp> #include <boost/multi_index/ordered_index.hpp>

delete a specific entry in the map,but the iterator must point to the next element after the deletion [duplicate]

蹲街弑〆低调 提交于 2019-12-23 22:24:28
问题 This question already has answers here : Closed 11 years ago . Duplicate: What happens if you call erase on a map element while iterating from begin to end How to filter items from a stdmap I have a map map1<string,vector<string>> i have a iterator for this map "itr". i want to delete the entry from this map which is pointed by "itr". i can use the function map1.erase(itr); after this line the iterator "itr" becomes invalid. as per my requirement in my project,the iterator must point to the

How to check if the first char in the line is # (beginning of a comment)

别来无恙 提交于 2019-12-23 21:41:18
问题 I have been following this convention thus far: std::string line; while(std::getline(in,line)) { if(line.size() && line[0] =='#') continue; /* parse text*/ } The obvious drawback is that comment may not begin at the first character, in the case of leading whitespace. What is the good way to deal with this sort of a thing? 回答1: Simple enhancement: you may want to use line.find_first_not_of(" ") to get the first non-whitespace and then check if that is a '#'. That would also cover to the zero

Split a line using std::regex and discard empty elements

夙愿已清 提交于 2019-12-23 20:37:44
问题 I need to split a line based on two separators: ' ' and ; . By example: input : " abc ; def hij klm " output: {"abc","def","hij","klm"} How can I fix the function below to discard the first empty element? std::vector<std::string> Split(std::string const& line) { std::regex seps("[ ;]+"); std::sregex_token_iterator rit(line.begin(), line.end(), seps, -1); return std::vector<std::string>(rit, std::sregex_token_iterator()); } // input : " abc ; def hij klm " // output: {"","abc","def","hij","klm