又重新敲了敲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 ;
}

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

05-11 13:45