又重新敲了敲c++primer上面的代码,觉得很有意思,讲的很细,c++真牛逼啊

 #include <iostream>
#include <string>
#include <cctype>
#include <vector>
#include <cassert>
using namespace std;
using std::ostringstream; int Add()
{
cout << "enter two numbers" << endl; int v1 = , v2 = ;
cin >> v1 >> v2;
cout << "the sum of" << v1 << "and" << v2
<< "is" << v1 + v2 << endl;
return ;
} int Unsigned()
{
unsigned u = , u2 = ;
cout << u2 - u << endl;
cout << u - u2 << endl; int i = , i1 = ;
cout << i - i1 << endl;
cout << i1 - i << endl; u = ;
i = ;
cout << i - u << endl;
cout << u - i << endl; u = ;
i = -;
cout << i + i << endl;
cout << u + i << endl; // u = 10;
// cout << "hehe" << endl;
// while (u >= 0)
// {
// cout << u << endl;
// u--;
// }
return ;
} int reused = ; int Scope_Levels()
{
int unique = ; cout << reused << "" << unique << endl;
int reused = ;
cout << reused << "" << unique << endl;
cout << ::reused << "" << unique << endl;
return ;
} int ReferenceTest()
{
int i = , &ri = i; cout << i << " " << ri << endl;
i = ;
cout << i << " " << ri << endl; ri = ;
cout << i << " " << ri << endl; return ;
} int CompoundRef()
{
int i = , *p = &i, &r = i;
cout << i << " " << *p << " " << r << endl; int j = , *p2 = &j;
int *&pref = p2;
cout << *pref << endl; pref = &i;
cout << *pref << endl; *pref = ;
cout << i << " " << *pref << " " << *p2 << endl; return ;
} // int Convs()
// {
// int i = 42;
// cout << i << endl;
//
// if (i)
// i = 0;
// cout << i << endl;
//
// bool b = 42;
// cout << b << endl;
//
// int j = b;
// cout << j << endl;
//
// double pi = 3.14;
// cout << pi << endl;
//
// j = pi;
// cout << j << endl;
//
// unsigned char c = -1;
// i = c;
// cout << i << endl;
//
// return 0;
// } int TestDoublePtr()
{
int ival = ;
int* pi = &ival;
int** ppi = &pi; cout << ival << " " << *pi << " " << **ppi << endl; int i = ;
int* p1 = &i; *p1 = *p1 * *p1;
cout << "i = " << i << endl; *p1 *= *p1;
cout << "i = " << i << endl;
return ;
} int TestDecltype()
{
int a = ;
decltype(a) c = a;
decltype((a)) d = a; c++;
cout << "a" << a << "c" << c << "d" << d << endl; d++;
cout << "a" << a << "c" << c << "d" << d << endl; int A = , B = ;
decltype((A)) C = A;
decltype(A = B) D = A; C++;
cout << A << C << D << endl;
D++;
cout << A << C << D << endl; return ;
} int TestCctype()
{
string s("Hello World!!!");
// for (auto c : s)
// cout << c << endl; string::size_type punct_cnt = ;
for (string::size_type c = ; c != s.size(); c++)
if (ispunct(s[c]))
++punct_cnt;
cout << punct_cnt << " " << s << endl; string orig = s;
for (string::size_type c = ; c != s.size(); c++)
s[c] = toupper(s[c]);
cout << s << endl; s = orig;
string::size_type index = ;
while (index != s.size() && !isspace(s[index]))
{
s[index] = toupper(s[index]);
index++;
}
cout << s << endl; return ;
} int TestArrayScores()
{
vector<unsigned> grades;
const size_t sz = ;
unsigned scores[sz] = {};
unsigned grade; while(cin >> grade)
{
if (grade <= )
scores[grade / ]++;
grades.push_back(grade);
}
cout << "grades.size = " << grades.size() << endl; for (vector<unsigned>::const_iterator g = grades.begin(); g != grades.end(); g++)
cout << *g << " ";
cout << endl; for (size_t i = ; i != sz; i++)
cout << scores[i] << " ";
cout << endl;
return ; } int TestGetline()
{
string line;
while (getline(cin, line))
cout << line << endl;
return ;
} int TestCstring()
{
string s1 = "A string example";
string s2 = "A different string"; if (s1 < s2)
cout << s1 << endl;
else
cout << s2 << endl; const char ca1[] = "A string example";
const char ca2[] = "A string example"; if (strcmp(ca1, ca2) < )
cout << ca1 << endl;
else
cout << ca2 << endl; const char *cp1 = ca1, *cp2 = ca2;
cout << strcmp(cp1, cp2) << endl;
cout << strcmp(cp2, cp1) << endl;
cout << strcmp(cp1, cp1) << endl; cout << strlen(cp1) << endl; const unsigned sz = + + ;
//char largeStr[sz];
//pass
return ;
} int TestIterator()
{
string str("some string"), orig = str;
if (!str.empty())
cout << str[] << endl;
if (!str.empty())
str[] = toupper(str[]);
cout << str << endl; str = orig; if (str.begin() != str.end())
{
string::iterator it = str.begin();
*it = toupper(*it);
}
cout << str << endl; str = orig;
for (string::size_type index = ; index != str.size() && !isspace(str[index]); index++)
str[index] = toupper(str[index]);
cout << str << endl; str = orig;
for (string::iterator it = str.begin(); it != str.end() && !isspace(*it); it++)
*it = toupper(*it);
cout << str << endl; str = orig;
string::size_type index = ;
while (index != str.size() && !isspace(str[index]))
{
str[index] = toupper(str[index]);
index++;
}
cout << str << endl; string::iterator beg = str.begin();
while (beg != str.end() && !isspace(*beg))
{
*beg = toupper(*beg);
beg++;
}
cout << str << endl; str = orig;
for (string::const_iterator c = str.begin(); c != str.end(); c++)
cout << *c;
cout << endl;
for (string::iterator c = str.begin(); c != str.end(); c++)
*c = '*';
cout << str << endl; str = orig;
for (string::size_type ix = ; ix != str.size(); ix++)
cout << str[ix];
for (string::size_type ix = ; ix != str.size(); ++ix)
str[ix] = '*';
cout << str << endl; return ; } int arr[];
int *p1[];
int(*p2)[] = &arr; typedef int arrT[]; arrT* func(int i);
int(*func(int i))[]; int odd[] = { , , , , };
int even[] = { , , , , }; int* elemPtr(int i)
{
return (i % ) ? odd : even;
} int(*arrPtr(int i))[]
{
return (i % ) ? &odd : &even;
} int (&arrRef(int i))[]
{
return (i % ) ? odd : even;
} int TestBefore1()
{
int* p = elemPtr();
int(*arrP)[] = arrPtr();
int(&arrR)[] = arrRef(); for (size_t i = ; i < ; i++)
cout << p[i];
cout << endl;
for (size_t i = ; i < ; i++)
cout << (*arrP)[i];
cout << endl;
for (size_t i = ; i < ; i++)
cout << arrR[i];
return ;
} // struct ErrCode
// {
// ErrCode(int i) : num(i) {}
// string msg()
// {
// ostringstream s;
// s << "ErrCode" << num;
// return s.str();
// }
// int num;
// }; string::size_type sumLength(const string&, const string&);
string::size_type latgerLength(const string&, const string&); string::size_type sumLength(const string& s1, const string& s2)
{
return s1.size() + s2.size();
} string::size_type largerLength(const string& s1, const string& s2)
{
return (s1.size() > s2.size()) ? s1.size() : s2.size();
} string::size_type(*getFcn(const string& fetch))(const string&, const string&)
{
if (fetch == "sum")
return sumLength;
return largerLength;
} int TestBefore2()
{
cout << getFcn("sum")("hello", "world") << endl;
cout << getFcn("larger")("hello", "world") << endl;
return ;
} inline const string&
shorterString(const string& s1, const string& s2)
{
return s1.size() <= s2.size() ? s1 : s2;
} int TestBefore3()
{
string s1("successes"), s2("failure");
cout << shorterString(s1, s2) << endl;
cout << shorterString(s1, s2).size() << endl;
cout << (s1.size() < s2.size() ? s1 : s2) << endl;
return ;
} void print(const int ia[], size_t size)
{
#ifndef NDEBUG
cerr << "print(int *, size_t)" << " " << size << endl;
#endif
} int TestErrorDeal()
{
string word = "foo";
const string::size_type threshold = ;
if (word.size() < threshold)
cerr << "Error: " << __FILE__
<< " : in function " << TestErrorDeal
<< " at line " << __LINE__ << endl
<< " Compiled on " << __DATE__
<< " at " << __TIME__ << endl
<< " Word read was \"" << word
<< "\": Length too short" << endl;
word = "something longer than five chars";
assert(word.size() > threshold); return ;
}

这些东西一定是敲过一遍才觉得有趣,当然上边的东西也很初级,这基本上是前四章内重要的函数了,把它们弄在了一起,暂时先不整理了

重新实践c++primer上面的代码的更多相关文章

  1. 抓取oschina上面的代码分享python块区下的 标题和对应URL

    # -*- coding=utf-8 -*- import requests,re from lxml import etree import sys reload(sys) sys.setdefau ...

  2. Git同步更新操作GitHub和码云仓库上面的代码

    一.前言 问题: 小编在生活中,一般都是将代码保存到github上,但由于国内的码云仓库确实速度比github快很多,用起来也很方便,于是后来就慢慢转码云了,当然小编在github上的代码也不想放弃更 ...

  3. VS2012如何更新下载TFS上面的代码到本地

    现在的代码基本上全都放在TFS上面,如何同步TFS上面的code呢? 1. Open "Team Explorer - Home" 2. Click "Source Co ...

  4. ajax同步导致ajax上面的代码不执行?

    js代码:环境:IE11要求:点击一个按钮后,页面xxx的地方立即显示"开始处理...",直到ajax处理结束后,xxx内容才更新为新的处理结果:点击事件执行代码如下:xxx.in ...

  5. 将GitLab上面的代码克隆到本地

    1.安装GitLab客户端 2.去GitLab服务端找项目路径 3.去GitLab客户端去克隆代码 右键-->git Clone 4.最后结果

  6. 10个造型奇特的css3进度条(有的html被编辑器转义了,上面的代码还是OK的)。。。转载

    <div id="caseVerte"> <div id="case1"></div> <div id="c ...

  7. Delphi 窗体失踪在最上面的代码

    unit ufrmSysPubMessage; interface uses  Windows, Forms, Messages, Classes, ExtCtrls, Controls, StdCt ...

  8. git clone 拉取github上面的代码报错:fatal: Authentication failed for xxx解决

    1.打开git bash,输入密码:git config --system --unset credential.helper2.结果报错:error: could not lock config f ...

  9. [SDK2.2]Windows Azure Storage (16) 使用WCF服务,将本地图片上传至Azure Storage (上) 客户端代码

    <Windows Azure Platform 系列文章目录> 前一章我们完成了服务器端的代码,并且已经发布到了Windows Azure云端. 本章我们将实现客户端的代码,客户端这里我们 ...

随机推荐

  1. GLSL纹理贴图 【转】

    转载:http://blog.csdn.net/hgl868/article/details/7872466 简单的纹理贴图(Simple Texture) 为了在GLSL中应用纹理,我们需要访问每个 ...

  2. 刘下记录:ImageView.scaleType 属性全解析(含视频)

    0. 译序 原文:Working with the ImageView ImageView 有一个经常使用场景:ImageView 的宽度固定,高度等比例缩放.且 ImageView 在 paddin ...

  3. 【前端阅读】——《活用PHP、MySQL建构Web世界》摘记之高级应用

    一.高级应用 1.计数器 计数器的原理很简单,只有两步: 第一步就是读写一个数字,第二步就是显示出来.一般CGI'大多直接写到文件系统,当然也可以利用MySQL来存储这个数字,完成第一步的操作. 第二 ...

  4. mpvue添加对scss的支持

    转载于:https://blog.csdn.net/butterfly5211314/article/details/80163628 新版的已经支持了,用npm官方即可下载,也就是 npm i sa ...

  5. {}在javascript与(python,java)中的含义区别

    {}在javascript中是对象,其访问属性的方法为 a.name,a['name'],参见http://www.itxueyuan.org/view/6332.html {}在python,jav ...

  6. vue.js+koa2项目实战(三)登录注册模态框

    登录注册模态框 注: [Vue warn]: Do not use built-in or reserved HTML elements as component id: diaLog 原因:diaL ...

  7. poj 1328 Radar Installation 【贪心】【区间选点问题】

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54798   Accepted: 12 ...

  8. Lua学习三----------Lua数据类型

    © 版权声明:本文为博主原创文章,转载请注明出处 Lua数据类型 - Lua是动态类型语言,不需要为变量定义类型,只需要为变量赋值 - Lua有8中基本数据类型:nil.boolean.number. ...

  9. Java 8 Collectors to Map

    1. 介绍 在本教程中,我们将讨论Collectors类的toMap()方法.我们使用它将流收集到一个Map实例中. 对于本教程中涉及的所有示例,我们将使用图书列表作为数据源,并将其转换为不同的Map ...

  10. caffe---ubuntu1604下anaconda2.5的尝试----失败,建议使用系统的python系统,避免各种各样的陷阱

    caffe---ubuntu1604下anaconda2.5的尝试----失败,建议使用系统的python系统,避免各种各样的陷阱. 如果使用caffe+ anacoanda 已经遇到的陷阱有 1. ...