std::vector<PersonNames> names = getNames();New Code:
for(int i=0; i<names.size(); i++)
{
names[i] += ';';
}
std::vector<PersonNames> names = getNames();In the new code, the iterator is removed which removes more clutter from the source code. Since the 'i' variable is removed, there is no way that the iterator is modified inside the loop, or has out of range bugs. It could also lead to better optimizations for the compiler. The range based for also works for standard C arrays, and also custom containers as long as begin() and end() are defined.
for(auto const &name : names)
{
name += ';';
}
bool ComponentTypes::anyComponentsDefined() constNew Code:
{
bool defined = false;
for(auto const &name : getComponentNames())
{
if(getComponentType(name.c_str()) != CT_Unknown)
{
defined = true;
break;
}
}
return defined;
}
bool ComponentTypes::anyComponentsDefined()This example checks if any elements in a vector have a certain type. The getComponentNames() function returns a "std::vector<std::string>".
{
auto const &names = getComponentNames();
return std::any_of(names.begin(), names.end(),
[=](std::string const &name) -> bool
{return(getComponentType(name.c_str()) != CT_Unknown);} );
}
 Some Test Functions:
          all_of, any_of, none_of
      
    
std::vector<std::string> packages = getPackages();Pre C++11 Code:
bool found = false;
for(int i=0; i<packages.size(); i++)
{
if(packages[i].mName.compare(name) == 0)
found = true;
break;
}
std::vector<std::string> packages = getPackages();More Advanced Example with C++11 Code:
bool found = std::find(packages.begin(), packages.end(), name) != packages.end());
auto const &supplierIter = std::find_if(packages.begin(), packages.end(),
[nodeName](Package const &pkg) -> bool
{ return(pkg.getPkgName().compare(nodeName) == 0); });
for(auto &fn : incs)New Lambda Code:
{
FilePath fp;
fp.getAbsolutePath(fn.c_str(), FP_Dir);
fn = fp.pathStdStr();
}
for_each(incs.begin(), incs.end(), [](std::string &fn)There is not much advantage to using for_each over range-based-for, except that it indicates that each element will be examined or modified, and a lambda can be used.
{
FilePath fp;
fp.getAbsolutePath(fn.c_str(), FP_Dir);
fn = fp.pathStdStr();
});
    static void removeLib(char const * const libName, std::set<std::string> &libDirs)
	{
	for(int i=0; i<libDirs.size(); i++)
	    {
	    if(libDirs.compare(libName) == 0)
	        clear(libDirs.begin()+i);
	    }
	}
    
    New Code:
    static void removeLib(char const * const libName, std::set<std::string> &libDirs)
	{
	libDirs.erase(std::remove_if(libDirs.begin(), libDirs.end(),
	    [=](std::string &str){ return(str.compare(libName) == 0); }), libDirs.end());
	}
    
    There are two common problems that can occur using remove_if. They can result in
    duplicate values in the vector.
	std::sort(mPackageNames.begin(), mPackageNames.end());
std::reverse(clumpNames.begin(), clumpNames.end());
std::vector<std::string> headers;Old Code:
std::copy_if(possibleHeaders.begin(), possibleHeaders.end(),
std::back_inserter(headers),
[](std::string const &header) { return isHeader(header.c_str()); });
std::vector<std::string> headers;This function copies only certain strings from one vector to another based on the results of the isHeader() function. The back_inserter will perform a push_back at the end of the target vector.
for(int i=0; i<possibleHeaders.size(); i++)
{
if(isHeader(possibleHeaders[i].c_str()))
{
headers.push_back(possibleHeaders[i]);
}
}
std::copy(sourceDirs.begin(), sourceDirs.end(), destDirs.begin());For vectors, use an std::back_inserter.
Create a functor:
template <typename Func> void processInts(std::vectorInvoke the functor:const &intCollection, Func procFunc) { for(auto const &intVal : intCollection) { // Some common special code can be placed here. procFunc(intVal); } } 
    void f()
        {
	std::vector numbers = { 3, 6, 9 };
        processInts(numbers, [](int val) { printf("%d\n", val); });
        }
  If the lambda modifies values, then add mutable:
        void f()
        {
	std::vector numbers = { 3, 6, 9 };
	// This modifies the values in "numbers"
	processInts(numbers, [](int &val) mutable { printf("%d\n", val++); });
        }
 
    And if the lambda modifies captured variable, then add "&":
    void f()
        {
	std::vector numbers = { 3, 6, 9 };
	int x = 0;
	// This modifies "x".
	processInts(numbers, [&x](int val) { printf("%d\n", x+=val); });
        }
 
    For a complete example, see oovcde/oovCommon/IncludeMap.cpp.
        class DebugResult
            {
            public:
                DebugResult(DebugResult &&src)
                    {
                    if(this != &src)
                        {
                        clear();
                        mChildResults = std::move(src.mChildResults);
                        }
                    }
            private:
                std::deque> mChildResults;
            }
        DebugResult srcResult;
        DebugResult res(std::move(srcResult));
     
    
    For a complete example, see oovEdit/Debugger.cpp and DebugResult.h/cpp.