1.第一个程序

         学习主要基于《Exploring C++》这本书有58讲,我大致按着它的顺序来,可能会插入一些额外的东西。

         这一讲的目的主要是用来测试编译器的,我创建了一个空的cpp文件但是发现怎么也运行不了。百度了之后才发现开始C++的编写不能只创建空文件,应该从创建project开始,具体内容网上很多(代码的运行似乎依附于一个主程序)。准备好了之后开始输入书上的第一个代码,运行,出错。

         先说说对代码的感受:和C++比起来python的代码舒服的多,不用打那么多括号,语句结束也不用分号,只是需要严格的缩进,这段代码还有很多看不太懂得,第一感觉就是比较繁杂。

        进行错误检查,发现焦点集中在

         if (not in)

        百般百度也查不到为什么,而且书上宣称这段代码是用来测试编译器是否合格的,而我的程序运行不了,这是不是意味着我的编译器不合格呢?(这本书还多次强调C++的国际标准)(顺便吐槽一下百度的搜索引擎,它完全把“c++”里的+当作是搜索的操作符,使得搜索结果的关联性极低,很难查到我要的,谷歌不存在这个问题)搞了一夜没搞懂,今天早上问了一个学过的C++的小伙伴,他说C++里没有“not”这个命令,只有“!”(存疑),把not 改成“!”之后果然可以运行了,这使我对这本书产生了一点疑惑(信赖程度下降),所幸,在测试这本书的其他代码时没有发现错误,于是继续愉快地使用这本书~

        (费解的是,这本书有诸多好评,没有人提到这个问题,求高手解答)


代码如下:

/** @file list0102.cpp */
/** Listing 1-2. 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);
}


转载于:https://my.oschina.net/xueyang/blog/204744

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值