您的位置:首页 > 编程语言 > C语言/C++

【CPP Training】Getting Started with C++ PA

2013-04-13 02:43 405 查看
      培训内容简要回顾

      1)学习C++之前需要思考的内容

            a)什么是编程

            b)有效的学习方式(思考、阅读、试验)

      2)C++的诞生与特点

      http://www.research.att.com/~bs/
      3)C++程序的基本结构

            a)最基本的代码

            b)创建源代码文件

            c)编译和链接

      4)C++能够做什么

            a)C++标准I/O库的学习

            b)string类以及标准模板库(STL)的学习

            c)C++泛型算法库的学习

      参考资料

      1)Standard C++ Library Reference

      http://www.cplusplus.com/reference/

      2)Third Party Library Reference

      http://en.cppreference.com/w/cpp/links/libs

      http://www.trumphurst.com/cpplibs/cpplibs.php

      编程练习题目



      程序的输入

GUOHONGYUN
LICHAO
XIAYUZHE
POSONG
LIUBO
LEIYU
ZHANGCANGJU
XIANGKAILIN
HUANGHAO
ZHUJIAJUN
ZHONGCHENG
LEIJUNAN
YANGBO
WEIWEI
LIUTINGWEI
OUYANGYU
ZHANGZHENG
ZHANGXINHAO
HOUTINGWU
KONGQIAO
SIXU
ZHANGJINJING
YANJUNHUA
HUANGRUI


      一种实现的方式

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

int main()
{
// Open the file "Names.txt" for input
std::ifstream inFile( "Names.txt" );
std::vector<std::string> vecNames;

// If the file is opened, then read all the names line by line
if ( inFile.is_open() )
{
std::string szName;

// If the end of the file is reached, the loop will stop
while ( inFile >> szName )
{
vecNames.push_back( szName );
}
}

// Sort the name in ascending order, and you may
// implement your own sorting algorithm as well
std::sort( vecNames.begin(), vecNames.end() );

// Open the file "NameSorted.txt" for output
std::ofstream outFile( "NameSorted.txt" );

// If the file is opened, then output the sorted names
if ( outFile.is_open() )
{
for ( int i = 0; i < vecNames.size(); i++ )
{
outFile << vecNames[i];

// At most five names per row
if ( 0 == ( i + 1 ) % 5 )
{
outFile << std::endl;
}
else
{
outFile << " ";
}
}
}

// Get a character from console preventing the immediate exit of the program
std::cin.get();

return 0;
}


      基于上述实现程序的输出

GUOHONGYUN HOUTINGWU HUANGHAO HUANGRUI KONGQIAO
LEIJUNAN LEIYU LICHAO LIUBO LIUTINGWEI
OUYANGYU POSONG SIXU WEIWEI XIANGKAILIN
XIAYUZHE YANGBO YANJUNHUA ZHANGCANGJU ZHANGJINJING
ZHANGXINHAO ZHANGZHENG ZHONGCHENG ZHUJIAJUN
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: