又重新敲了敲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. django导出数据到excel

    import xlwt,StringIodef dumpData(request): #获取数据 activitys = Activity.objects.all().order_by('id') i ...

  2. sublime text常用快捷键(转)

    选择一个选中项的下一个匹配项: ctrl+d 把光标放在一个单词上,按下ctrl+ D,将选择这个单词.一直按住ctrl且按D多次,将选择当前选中项的下一个匹配项.通过按住ctrl,再按D三次,将选择 ...

  3. 关于embedding-深度学习基本操作 【Word2vec, Item2vec,graph embedding】

    https://zhuanlan.zhihu.com/p/26306795 https://arxiv.org/pdf/1411.2738.pdf https://zhuanlan.zhihu.com ...

  4. Node.js学习入门手册

    Node.js 安装 1.下载http://nodejs.org/dist/v0.12.1/node-v0.12.1-x86.msi并完成安装 2.下载https://www.python.org/f ...

  5. MAC - 命令行中用sublime打开指定文件,使用ln命令建立软链接

    眼下sublime是mac下最好的文本编辑软件.常常要使用它打开一些文件,比如html,js,txt,json等文件,可是sublime2默认不支持在命令行下调用.经过研究发现能够用建立软连接的方式调 ...

  6. Vue 引入ElementUI 2.0.11:依赖未发现的问题

    转自:https://blog.csdn.net/cslucifer/article/details/79019649

  7. highcharts 绘制图标的JAVASCRIPT 类库 收藏

    官方站点 : http://www.highcharts.com 演示样例网址 : http://www.highcharts.com

  8. python 和 mysql连接

    python 和 mysql连接 虫师教程:http://www.cnblogs.com/fnng/p/3565912.html 其他教程pymysql:http://www.cnblogs.com/ ...

  9. 跟我学AngularJs:Controller数据共享、继承、通信使用具体解释

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主讲了AngularJs中的Controller中数据共享.继承.通信的具体使用 本 ...

  10. PHP通过prepare执行查询取得数据

    可以用来防止sql注入 <?php $pdo=new PDO("mysql:host=localhost;dbname=itest", 'root',''); //先构建查询 ...