Complexity exists underneath so that clarity can exist above.
Ever look at how some of the things in the Standard Library or Boost are actually implemented? It's pretty scary, actually.
But that means that as the
user of the library, life is oh so much easier.
This is exactly the point of OP's post.
The trick in C++ is that there is not a lot of overhead in startup -- only those things you ask for are available. Thus, if you wish to do something like get a random number, you must first load/initialize a library for it. Do that by using the correct constructs:
1 2 3 4 5 6
|
#include "my_awesome_RNG_frobber.hpp"
int main()
{
std::cout << "Today your lucky number is " << awesome::get_random_int_in_range( 1, 100 ) << "\n";
}
|
All the initialization magic can be done behind the scenes, just like it is done with
std::cout
.
The power of C++ syntax lies in making things work simpler. For example, you can certainly provide your own methods to compare strings, say, case-insensitively:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <algorithm>
#include <cctype>
#include <ciso646>
#include <iostream>
#include <string>
struct nocase
{
const std::string& s;
nocase(): s("") { }
nocase( const std::string& s ): s(s) { }
static bool is_less_than( const std::string& a, const std::string& b )
{
auto ai = a.begin();
auto bi = b.begin();
while (ai != a.end() and bi != b.end())
if (std::tolower( *ai++ ) != std::tolower( *bi++ ))
return (*--ai < *--bi);
return a.size() < b.size();
}
static bool is_equal( const std::string& a, const std::string& b )
{
if (a.size() != b.size()) return false;
return mismatch( a.begin(), a.end(), b.begin(),
[]( char a, char b ) { return std::tolower( a ) == std::tolower( b ); }
).first == a.end();
}
};
bool operator < ( const nocase& a, const std::string& b ) { return nocase::is_less_than( a.s, b ); }
bool operator < ( const std::string& a, const nocase& b ) { return nocase::is_less_than( a, b.s ); }
bool operator < ( const nocase& a, const nocase& b ) { return nocase::is_less_than( a.s, b.s ); }
bool operator == ( const nocase& a, const std::string& b ) { return nocase::is_equal( a.s, b ); }
bool operator == ( const std::string& a, const nocase& b ) { return nocase::is_equal( a, b.s ); }
bool operator == ( const nocase& a, const nocase& b ) { return nocase::is_equal( a.s, b.s ); }
int main()
{
std::string s1, s2;
std::cout << "s1? "; getline( std::cin, s1 );
std::cout << "s2? "; getline( std::cin, s2 );
if (nocase(s1) == s2)
{
if (s1 == s2) std::cout << "a perfect match!\n";
else std::cout << "match!\n";
}
else std::cout << "no match\n";
}
|
All that stuff at the top is full of boilerplate and seems like a lot of code.
But tuck that all in a library somewhere. What matters is down below in main(), line 48, where life looks pretty and easy.
* This is a very simple example, and does not exemplify a lot of things that you should do, nor does it have anywhere near the complexity necessary to properly compare two strings without case-sensitivity. JSYK.
And this gets back to one of the core principles of C++: get a library to do that. If you haven't installed Boost, do it. There are a lot of things you can do to make things prettier and easier.
The 'foreach' issue was so big that Boost had a FOREACH macro, which was often used like this:
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH
int main()
{
std::vector<int> xs ...;
foreach (int x, xs)
{
do_something_with( x );
}
}
|
So very useful, that now it is programmed into the language:
1 2 3 4 5 6 7 8 9
|
int main()
{
std::vector<int> xs ...;
for (int x : xs)
{
do_something_with( x );
}
}
|
Backporting? Easy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#ifdef BOOST_FOREACH
#define for_ BOOST_FOREACH
#else
#define for_( x, xs ) for ( x : xs )
#endif
int main()
{
std::vector<int> xs ...
for_( int x, xs )
{
do_something_with( x );
}
}
|
All the complexity belongs in libraries. Your code
should be pretty!
This has been a rambling post, full of randomness. Your random number for today is 92.