I just thought since I had it, maybe I'd share it?
Your First Program
Now that you have you have your recommended tools for C++, Fire up your favorite text editor or your C++ IDE and start your first project or create a new file. Name this file list0101.cpp, which is short for listing 1-1. Several different file name extensions are popular for C++ programs. I like to use .cpp, where the "p" means "plus". Other common extensions are .cxx and .cc. Some compilers recognize .C (uppercase C) as a C++ file extension, but I don't recommend using it because it is too easy to confuse with .c (lowercase c), the default extensions for C programs. Many desktop environments do not distinguish between uppercase and lowercase file names, further compounding the problem. Pick your favorite and stick with it. Type in the text contained within Listing 1-1. (With one exception, you can download all the code list for below. I want you to get used to typing C++ code in your text editor.)
Code 1-1 ~ Your first Program
------
Alright thats great, Now go back and double-check your source code. Make sure you entered everything correctly.
Did you actually double-check the program?____________
Did you find any typos that needed correcting? _________
No doubt, some of this code is gibberish to you. That's okay. The point of this exercise is not to understand C++, but to make sure you can use your tools properly. The comments describe the program, which is a simple sort of utility. I could have started with a trivial, "Hello, world" type of program, but that touches only a tiny fraction of the language and library. This program, being slightly more complex, does a better job at revealing possible installation or other problems with your tools.
~End of tutorial one!
~Press the like button!
Your First Program
Now that you have you have your recommended tools for C++, Fire up your favorite text editor or your C++ IDE and start your first project or create a new file. Name this file list0101.cpp, which is short for listing 1-1. Several different file name extensions are popular for C++ programs. I like to use .cpp, where the "p" means "plus". Other common extensions are .cxx and .cc. Some compilers recognize .C (uppercase C) as a C++ file extension, but I don't recommend using it because it is too easy to confuse with .c (lowercase c), the default extensions for C programs. Many desktop environments do not distinguish between uppercase and lowercase file names, further compounding the problem. Pick your favorite and stick with it. Type in the text contained within Listing 1-1. (With one exception, you can download all the code list for below. I want you to get used to typing C++ code in your text editor.)
Code 1-1 ~ Your first Program
------
Code:
/** Listing 1-1. Testing Your Compiler */
/// Sort the standard input alphabetically.
/// Read lines of text, sort them, and print the results to the standard output.
/// If the command line names a file, read from that file. Otherwise, read from
/// the standard input. The entire input is stored in memory, so don’t try
/// this with input files that exceed available RAM.
///
/// Comparison uses a locale named on the command line, or the default, unnamed
/// locale if no locale is named on the command line.
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <locale>
#include <ostream>
#include <string>
#include <vector>
/// Read lines of text from @p in to @p iter. Lines are appended to @p iter.
/// @param in the input stream
/// @param iter an output iterator
template<class Ch, class Tr, class OutIter>
void read(std::basic_istream<Ch,Tr>& in, OutIter iter)
{
std::basic_string<Ch,Tr> line;
while (std::getline(in, line))
{
*iter = line;
++iter;
}
}
/// Sorter function object.
/// Parameterize the class with the character type used for strings.
/// The sorter object caches a locale and its @c collate facet, to use for
/// comparing strings.
template<typename Ch>
class sorter
{
public:
/// Construct the sorter object, caching the locale that has the given name.
/// @param locname The name of the locale to cache.
sorter(Ch const* locname) :
loc_(std::locale(locname)),
collate_(std::use_facet<std::collate<Ch> >(loc_))
{}
/// Construct a default sorter object, using the global locale.
sorter() :
loc_(std::locale()),
collate_(std::use_facet<std::collate<Ch> >(loc_))
{}
/// Compare for less-than, for use as a comparison predicate with any
/// standard algorithm.
/// @param lhs left-hand side operand
/// @param rhs right-hand side operand
/// @return true if @p lhs < @p rhs in the given locale, false otherwise
template<typename Tr>
bool operator()(const std::basic_string<Ch,Tr>& lhs,
const std::basic_string<Ch,Tr>& rhs)
{
return collate_.compare(lhs.data(), lhs.data()+lhs.size(),
rhs.data(), rhs.data()+rhs.size()) < 0;
}
private:
std::locale loc_; ///< cached locale
const std::collate<Ch>& collate_; ///< cached @c collate facet
};
/// Make a sorter object by deducing the template parameter.
/// @param name the locale name to pass to the sorter constructor
/// @return a new sorter object
template<typename Ch>
sorter<Ch> make_sorter(Ch const * name)
{
return sorter<Ch>(name);
}
/// Main program.
int main(int argc, char* argv[])
try
{
// Throw an exception if an unrecoverable input error occurs, e.g.,
// disk failure.
std::cin.exceptions(std::ios_base::badbit);
// Part 1. Read the entire input into text. If the command line names a file,
// read that file. Otherwise, read the standard input.
std::vector<std::string> text; ///< Store the lines of text here
if (argc < 2)
read(std::cin, std::back_inserter(text));
else
{
std::ifstream in(argv[1]);
if (not in)
{
std::perror(argv[1]);
return EXIT_FAILURE;
}
read(in, std::back_inserter(text));
}
// Part 2. Sort the text. The second command line argument, if present,
// names a locale, to control the sort order. Without a command line
// argument, use the default locale (which is obtained from the OS).
std::sort(text.begin(), text.end(), make_sorter(argc >= 3 ? argv[2] : "" ));
// Part 3. Print the sorted text.
std::copy(text.begin(), text.end(),
std::ostream_iterator<std::string>(std::cout, "\n"));
}
catch (std::exception& ex)
{
std::cerr << "Caught exception: " << ex.what() << '\n';
std::cerr << "Terminating program.\n";
std::exit(EXIT_FAILURE);
}
catch (...)
{
std::cerr << "Caught unknown exception type.\nTerminating program.\n";
std::exit(EXIT_FAILURE);
}
Alright thats great, Now go back and double-check your source code. Make sure you entered everything correctly.
Did you actually double-check the program?____________
Did you find any typos that needed correcting? _________
No doubt, some of this code is gibberish to you. That's okay. The point of this exercise is not to understand C++, but to make sure you can use your tools properly. The comments describe the program, which is a simple sort of utility. I could have started with a trivial, "Hello, world" type of program, but that touches only a tiny fraction of the language and library. This program, being slightly more complex, does a better job at revealing possible installation or other problems with your tools.
~End of tutorial one!
~Press the like button!
Last edited: