victorio wrote: |
---|
I need to find a substring. |
@malibor:
A word was requested, and that is what find_if() returns: first "valid" word (or none).
"All words that contain substring" is different goal.
T x = {};
declares variable named 'x' that has type 'T' and initializes it with empty value.
C++11 allows also syntax:
T x {};
However, the std::vector is a class that has
default constructor. The initializer is unnecessary.
1 2 3
|
for ( const auto& ref : finds ) {
std::cout << *ref << std::endl;
}
|
This is
ranged for
-syntax that was added by C++11.
1 2 3 4 5 6 7 8 9 10 11
|
vector<string> words;
// ranged for
for ( string w : words ) {
std::cout << w << std::endl;
}
// old for
for ( size_t x=0; x < words.size(); ++x ) {
string w = words[x];
std::cout << w << std::endl;
}
|
The
auto
keyword in C++11 (and later) makes the compiler deduce typename. The 'finds' has type
std::vector<const std::string*>
and thus 'auto' in the loop (probably) means
const std::string*
Note: Constant reference to a pointer yields no advantage in this case.