Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
    my english is poor
    Grant Rostig
    My code was recently criticized for using a while loop instead of a for loop. I was told that incrementing my loop variable (iterator) at the bottom of the loop was more likely to become buggy due to later sloppy code changes. Also told that the c++ optimizer might have more trouble optimizing and "un-rolling" the loop. On the other hand, the only way to use a for loop was to iterate in reverse, since elements of the vector were being deleted. I figured that was pretty ugly, especially with the "-1" required for loop conditional in the for solution. Opinions? So is "while" looked down upon in Modern C++?
    Darrell Wright
    @grantrostig what about vect.erase( std::remove_if( vect.begin( ), vect.end( ), predicate ), vect.end( ) );
    Clément Grégoire
    Just use reverse iterators ?
    While loop have specific purposes, iterating on a known range is more of a 'for' use case even in reverse order imo
    While loop are best used on containers when you dont know the range / number of iterations
    Konstantin Frumkin
    Great idea to set a collection of best practices of using c++.
    Konstantin Frumkin
    Having moved to c++14 I faced with problem that for best code it is nice to use templates with universal references, but do not understand clearly how to involve coding style with templates and classes' methods the right way. It makes shit code if I put templates methods into class. I have to move implementation of templates in the additional file and some more bad looking code.
    Is there since of using universal references this way. I do not know.
    Grant Rostig
    @Lectem thanks for the advice.
    Grant Rostig
    @beached , nice STL solution. However I was iterating because I needed to do another operation in addition to the vect.erase(). The idea is to find specific elements and move them to a destination vector and them immediately erase these elements from the source vector. Do you know of a STL approach to that scenario? I don't want to first remove the elements and then erase them in another loop (or algorithm).
    Darrell Wright
    @grantrostig maybe copy the item in the predicate? if( some true thing ) { othervector.push_back( item ); return true; } return false....
    Haven't tested though
    @grantrostig actually, you cannot do a move there. but you can do a copy. or another option is to use std::partition and then use the returned iterator as the start of the copy to the new vector and then do the erase with it after
    auto new_end = std::partition( vect.begin( ), vect.end( ), predicate );
    std::copy( new_end, vect.end( ), std::back_inserter( some_other_vector ) );
    vect.erase( new_end, vect.end( ) );
    Darrell Wright
    @grantrostig if you have c++ 17 then std::remove_copy_if might do it too
    Hi, I have to present c++ best practices to my colleagues who are mostly beginners or intermediate, what references would you recommend?
    Jason Turner
    you can start at cppbestpractices.com
    Victor Queiroz
    Do you think std::variant is a good practice?
    Jason Turner
    It certainly can be. It can be used in low-zero overhead ways
    depends on what you need to do
    Joseph Nicholas R. Alcantara
    Hi @lefticus I don’t know if this channel is stil active, but is there a introduction of using constexpr?
    Mateusz Łoskot
    Scroll up, compare dates
    Stephan Grein
    Pavlos Kokozidis
    Grant Rostig
    Worldwide online c++ meetup starting in 1 hour Austin, TX time here: https://www.meetup.com/cppmsg/events/sfqzfsyccfbnb/
    anyone known about boost-ublas?