又重新敲了敲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. DIY树莓派之随身工具箱

    摆弄树莓派有一年多了,在这里把经验分享给大家,少走弯路. 先放图两张. 搭建目的: wifi信号中转站\网站服务器\IC卡渗透测试\中间人\otr… 基于树莓派3 系统为Kali Linux 2017 ...

  2. MySQL监控工具——innotop

    MySQL监控工具--innotop innotop是一个mysql数据库实时监控工具,其功能强大,信息种类繁多,很能体现数据库的状态. 它实际上是一个perl脚本,整合show status/sho ...

  3. 转: 低延迟系统的Java实践

    from:  http://blog.csdn.net/jacktan/article/details/41177779 在很久很久以前,如果有人让我用Java语言开发一个低延迟系统,我肯定会用迷茫的 ...

  4. Mongodb性能调优

    摘要 1. Mongodb 适用场景简介 2. Mongodb 性能监控与分析 3. Mongodb 性能优化建议 关于Mongodb的几个大事件 1.根据美国数据库知识大全官网发布的DB热度排行,M ...

  5. 【LeetCode】Validate Binary Search Tree ——合法二叉树

    [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

  6. 阿里巴巴天猫超市团队招聘java开发工程师

    大家好,发个招聘信息:我是阿里巴巴集天猫超市开发团队的同学,我们部门目前在杭州招人,P6岗位,要求至少本科,熟悉java,spring等java开发技术,最好有互联网企业开发经验,感兴趣的可以通过我直 ...

  7. bzoj4010【HNOI2015】菜肴制作

    4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec  Memory Limit: 512 MB Submit: 981  Solved: 480 [Submit][Statu ...

  8. HDU 3416

    Marriage Match IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  9. Asp.Net北大青鸟总结(四)-使用GridView实现真假分页

    这段时间看完了asp.net视频.可是感觉到自己的学习好像没有巩固好,于是又在图书馆里借了几本关于asp.net的书感觉真的非常好自己大概对于asp.net可以实现主要的小Demo.可是我知道仅仅有真 ...

  10. 【转载】【selenium+Python WebDriver】之selenium的定位以及切换frame(iframe)

    感谢CSDN:huilan_same大神 网页地址:http://blog.csdn.net/huilan_same/article/details/52200586