重新实践c++primer上面的代码
又重新敲了敲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 = π 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上面的代码的更多相关文章
- 抓取oschina上面的代码分享python块区下的 标题和对应URL
# -*- coding=utf-8 -*- import requests,re from lxml import etree import sys reload(sys) sys.setdefau ...
- Git同步更新操作GitHub和码云仓库上面的代码
一.前言 问题: 小编在生活中,一般都是将代码保存到github上,但由于国内的码云仓库确实速度比github快很多,用起来也很方便,于是后来就慢慢转码云了,当然小编在github上的代码也不想放弃更 ...
- VS2012如何更新下载TFS上面的代码到本地
现在的代码基本上全都放在TFS上面,如何同步TFS上面的code呢? 1. Open "Team Explorer - Home" 2. Click "Source Co ...
- ajax同步导致ajax上面的代码不执行?
js代码:环境:IE11要求:点击一个按钮后,页面xxx的地方立即显示"开始处理...",直到ajax处理结束后,xxx内容才更新为新的处理结果:点击事件执行代码如下:xxx.in ...
- 将GitLab上面的代码克隆到本地
1.安装GitLab客户端 2.去GitLab服务端找项目路径 3.去GitLab客户端去克隆代码 右键-->git Clone 4.最后结果
- 10个造型奇特的css3进度条(有的html被编辑器转义了,上面的代码还是OK的)。。。转载
<div id="caseVerte"> <div id="case1"></div> <div id="c ...
- Delphi 窗体失踪在最上面的代码
unit ufrmSysPubMessage; interface uses Windows, Forms, Messages, Classes, ExtCtrls, Controls, StdCt ...
- git clone 拉取github上面的代码报错:fatal: Authentication failed for xxx解决
1.打开git bash,输入密码:git config --system --unset credential.helper2.结果报错:error: could not lock config f ...
- [SDK2.2]Windows Azure Storage (16) 使用WCF服务,将本地图片上传至Azure Storage (上) 客户端代码
<Windows Azure Platform 系列文章目录> 前一章我们完成了服务器端的代码,并且已经发布到了Windows Azure云端. 本章我们将实现客户端的代码,客户端这里我们 ...
随机推荐
- Asp.net对文件夹和文件的操作类
using System; using System.IO; using System.Web; namespace SEC { /**//// /// 对文件和文件夹的操作类 /// public ...
- git 服务器搭建,在自己服务器上搭建私有仓库
创建一个简单的私人Git版本控制服务器,首先得有个服务器(屁话).这种方式适合人比较少的情况,管理不需要很复杂,只要增加几个账号就能搞定. 如下面的情况,有一个服务器,两个客户端. 服务器:Debia ...
- vue2.0 仿手机新闻站(六)详情页制作
1.结构 2.配置详情页路由 router.config.js /** * 配置 路由 */ // 导入组件 import Home from './components/Home.vue' impo ...
- 使用JDK中的类URL访问HDFS(来自吴超Hadoop)
package hdfs; import java.io.InputStream; import java.net.URL; import org.apache.hadoop.fs.FsUrlStre ...
- jenkins 构建一个前端web项目
Jenkins发布web前端代码 “系统管理”“管理插件”“已安装” 检查是否有“Git plugin”和“Publish Over SSH”两个插件,如果没有,则需点击“可选插件”,找到它并安装 ...
- Html5 播放Hls格式视频
二群号为766718184 ,博主提供Ffmpeg.GB28181视频教程 播放地址: http://www.iqiyi.com/u/1426749687 移动端Html5支持Hls格式视频播放,创 ...
- [ 转]C++ 虚函数表解析
http://blog.csdn.net/haoel/article/details/1948051 前言 C++中的虚函数的作用主要是实现了多态的机制.关于多态,简而言之就是用父类型别的指针指向其子 ...
- MySql(四):备份与恢复
一.数据库备份使用场景 下面我就列举一下我个人理解的我们能够需要用到数据库备份的一些比较常见的情况吧. a.数据丢失应用场景 1.人为操作失误造成某些数据被误操作:2.软件BUG 造成数据部分或者全部 ...
- IPv4(三)地址掩码
回顾网络类型确定 回顾一下之前学过的如果确定IP地址网络号,这里先不考虑子网. 首先通过首个八位组字节规则很容易确定IP地址属于那个网络: 如果第1位是0,则是A类地址: 如果前两位是10,则是B类地 ...
- Linux下服务端口被占用
有一次,在启动ejabberd的时候,报错如下: 10:30:15 =CRASH REPORT==== crasher: initial call: supervisor:ejabberd_liste ...