stl

Is it possible to erase elements of a std::list in a c++11 for each loop

蹲街弑〆低调 提交于 2019-12-23 07:32:41
问题 I want to use the new C++11 for each loop to iterate over all elements of a list and erase certains elements. For example std::list<int> myList; myList.push_back(1); myList.push_back(13); myList.push_back(9); myList.push_back(4); for(int element : myList) { if(element > 5) { //Do something with the element //erase the element }else{ //Do something else with the element } } Is it possible to do this using the for each loop or do I have to go back to iterators to achive this? 回答1: You should be

Is it possible to erase elements of a std::list in a c++11 for each loop

◇◆丶佛笑我妖孽 提交于 2019-12-23 07:32:30
问题 I want to use the new C++11 for each loop to iterate over all elements of a list and erase certains elements. For example std::list<int> myList; myList.push_back(1); myList.push_back(13); myList.push_back(9); myList.push_back(4); for(int element : myList) { if(element > 5) { //Do something with the element //erase the element }else{ //Do something else with the element } } Is it possible to do this using the for each loop or do I have to go back to iterators to achive this? 回答1: You should be

STL “erase-remove” idiom: Why not “resize-remove”?

99封情书 提交于 2019-12-23 07:25:41
问题 It is commonly understood that a good way to fully delete desired items from a std::vector is the erase-remove idiom. As noted in the above link (as of the date of this posting), in code the erase-remove idiom looks like this: int main() { // initialises a vector that holds the numbers from 0-9. std::vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // erase-remove idiom to completely eliminate the desired items from the vector v.erase( std::remove( std::begin(v), std::end(v), 5 ), std::end(v

STL “erase-remove” idiom: Why not “resize-remove”?

一个人想着一个人 提交于 2019-12-23 07:24:53
问题 It is commonly understood that a good way to fully delete desired items from a std::vector is the erase-remove idiom. As noted in the above link (as of the date of this posting), in code the erase-remove idiom looks like this: int main() { // initialises a vector that holds the numbers from 0-9. std::vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // erase-remove idiom to completely eliminate the desired items from the vector v.erase( std::remove( std::begin(v), std::end(v), 5 ), std::end(v

Passing array as function parameter in C++ [duplicate]

强颜欢笑 提交于 2019-12-23 07:03:57
问题 This question already has answers here : When a function has a specific-size array parameter, why is it replaced with a pointer? (3 answers) Closed 4 years ago . I am aware that an array can be passed to a function in quite a few ways. #include <iostream> #include <utility> using namespace std; pair<int, int> problem1(int a[]); int main() { int a[] = { 10, 7, 3, 5, 8, 2, 9 }; pair<int, int> p = problem1(a); cout << "Max =" << p.first << endl; cout << "Min =" << p.second << endl; getchar();

Do data members form a range?

僤鯓⒐⒋嵵緔 提交于 2019-12-23 06:53:58
问题 Can I treat consecutive data members of the same type as a range? For example: struct X { int a, b, c, d, e; }; X x = {42, 13, 97, 11, 31}; std::sort(&x.a, &x.a + 5); // kosher? 回答1: No, this is undefined behaviour. You are treating x.a like the first element of an array, which it isn't. May work on some implementations, may raid your fridge too ;) 回答2: Don't do that. Compiler is free to add paddings between structure members(and at the end). 回答3: If this is really something you want to do,

which is better in general, map or vector in c++?

社会主义新天地 提交于 2019-12-23 06:14:12
问题 As I know that accessing an element in vector takes constant time while in map takes logarithmic time. However, storing a map takes less memory than storing a vector. Therefore, I want to ask which one is better in general? I'm considering using one of those two in my program, which has about 1000 elements. I plan to use 3 dimensional vector, which would take 1000x1000x1000 elements. 回答1: There is no correct answer to this question. The correct question should be "which is better for the

stl “vector<T> too long”

此生再无相见时 提交于 2019-12-23 06:12:17
问题 i read in other answers that theres no limit imposed by c++ compiler maximum size of std::vector. i am trying to use vector for one purpose, and in need to have 10^19 items. typedef struct{ unsigned long price, weight; }product; //inside main unsigned long long n = 930033404565174954; vector<product> psorted(n); the program breaks on the last statement. if i try resize(n) instead of initializing with n then also program breaks with message : vector<T> too long std::length_error at memory

C++ standard list and default-constructible types

青春壹個敷衍的年華 提交于 2019-12-23 05:38:11
问题 Why is that the single parameter constructor of std::list<T> requires T to be a default-constructible type? I mean the following code does not compile. struct Foo { // does not have default constructor. Foo (int i) {} } int main(void) { std::list<Foo> l(10); } It seems possible to use the construct and destroy idioms as they have already done in the std::vector, albeit with more book-keeping the list class. On a related note, why not have the capacity function in list? You can argue that such

Why this thread safe queue, creates a deadlock?

本小妞迷上赌 提交于 2019-12-23 05:30:27
问题 I've written my own version of thread safe queue. However, when I run this program, it hangs/deadlocks itself. Wondering, why is this locks/hangs forever. void concurrentqueue::addtoQueue(const int number) { locker currentlock(lock_for_queue); numberlist.push(number); pthread_cond_signal(&queue_availability_condition); } int concurrentqueue::getFromQueue() { int number = 0; locker currentlock(lock_for_queue); if ( empty() ) { pthread_cond_wait(&queue_availability_condition,&lock_for_queue); }