这是一篇听课笔记,这里是课程链接
# include <iostream>
# include <deque>
int main()
{
using namespace std;
deque<int> a;
a.push_back(3);
a.push_back(4);
a.push_back(5);
a.push_front(2);
a.push_front(0);
for (size_t ncount = 0; ncount < a.size(); ++ncount)
{
cout << a[ncount] << endl;
}
}
# include <iostream>
# include <deque>
# include <algorithm>
int main()
{
using namespace std;
deque<int> a;
a.push_back(3);
a.push_back(4);
a.push_back(5);
a.push_front(2);
a.push_front(0);
for (size_t ncount = 0; ncount < a.size(); ++ncount)
{
cout <<"a["<<ncount << "] = " << a[ncount] << endl;
}
cout << endl << endl;
a.pop_back();
a.pop_front();
deque<int>::iterator iElement;
for (iElement = a.begin();
iElement != a.end();
++iElement)
{
size_t nOffset = distance(a.begin(), iElement);
cout << "a[" << nOffset << "]=" << *iElement << endl;
}
cout << endl;
return 0;
}
#include <iostream>
#include <list>
using namespace std;
void PrintListContents(const list<int>& listInput);
int main()
{
list<int> a;
list<int> b;
std::list<int>::iterator iter;
a.push_front(3);
a.push_front(2);
a.push_front(1);
a.push_back(4);
PrintListContents(a);
cout << endl;
iter = a.begin();
a.insert(iter, 10);
PrintListContents(a);
++iter;
a.insert(iter, 2, 0);
PrintListContents(a);
b.push_back(200);
b.push_back(300);
b.push_back(400);
a.insert(a.begin(), b.begin(), b.end());
PrintListContents(a);
a.insert(a.end(), b.begin(), b.end());
PrintListContents(a);
a.insert(a.begin(), ++b.begin(), --b.end());
PrintListContents(a);
return 0;
}
void PrintListContents(const list<int>& listInput)
{
cout << "{" ;
std::list<int>::const_iterator iter;
for(iter = listInput.begin(); iter!=listInput.end(); ++iter)
{
cout << *iter <<" ";
}
cout << "}" << endl;
}
#include <iostream>
#include <list>
using namespace std;
void PrintListContents(const list<int>& listInput);
int main()
{
list<int> a;
std::list<int>::iterator iter2;
a.push_front(3);
a.push_back(4);
iter2 = a.insert(a.begin(), 2);
a.push_front(0);
PrintListContents(a);
a.erase(iter2, a.end());
PrintListContents(a);
cout << endl;
return 0;
}
void PrintListContents(const list<int>& listInput)
{
cout << "{" ;
std::list<int>::const_iterator iter;
for(iter = listInput.begin(); iter!=listInput.end(); ++iter)
{
cout << *iter <<" ";
}
cout << "}" << endl;
}
#include <iostream>
#include <list>
using namespace std;
void PrintListContents(const list<int>& listInput);
int main()
{
list<int> a;
list<int> b;
a.push_front(3);
a.push_back(4);
a.push_front(2);
PrintListContents(a);
a.reverse();
PrintListContents(a);
b.push_back(5);
b.push_back(3);
b.push_back(6);
PrintListContents(b);
b.sort();
PrintListContents(b);
cout << endl;
return 0;
}
void PrintListContents(const list<int>& listInput)
{
cout << "{" ;
std::list<int>::const_iterator iter;
for(iter = listInput.begin(); iter!=listInput.end(); ++iter)
{
cout << *iter <<" ";
}
cout << "}" << endl;
}
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<int> d;
d.push(20);
d.push(10);
d.push(5);
cout << d.top() << endl;
return 0;
}
#include <iostream>;
#include<queue>;
#include <list>;
using namespace std;
int main()
{
queue<int, deque<int>> a;
queue<int, list<int>> b;
a.push(2);
a.push(3);
a.push(4);
cout << "# : " << a.size() << endl;
cout << "the element in the front is " << a.front() << endl;
cout << "the element at the back is " << a.back() << endl;
a.pop();
cout << "after pop, " << endl;
cout << "# : " << a.size() << endl;
cout << "the element in the front is " << a.front() << endl;
}
#include <iostream>;
#include <queue>;
#include <deque>;
using namespace std;
int main()
{
priority_queue<int> pq;
priority_queue<int, deque<int>, greater<int>> pq2;
pq.push(10);
pq.push(5);
pq.push(20);
cout << "the most front element is " << pq.top() << endl;
while (!pq.empty())
{
cout << " delete the element " << pq.top() << endl;
pq.pop();
}
pq2.push(30);
pq2.push(10);
pq2.push(15);
cout << "the most front element is " << pq2.top() << endl;
while (pq2.size() != 0)
{
cout << pq2.top() << endl;
pq2.pop();
}
return 0;
}
C++ Primer中级
类的定义
# include <iostream>
#include <string>
using namespace std;
class Sales_item
{
public:
Sales_item(const string &book, unsigned units, double amount)
:isbn(book),units_sold(units),revenue(amount)
{}
double avg_price() const
{
if (units_sold)
return revenue / units_sold;
else
return 0;
}
bool same_isbn(const Sales_item &rhs) const
{
return isbn == rhs.isbn;
}
void add(const Sales_item &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
}
private:
string isbn;
unsigned units_sold;
double revenue;
};
int main()
{
Sales_item x(string("101-102"), 2, 40);
Sales_item y(string("101-102"), 4, 60);
cout << "x 's avg_price is " << x.avg_price() << endl;
if (x.same_isbn(y))
x.add(y);
cout << "after adding y, " << "x 's avg_price is " << x.avg_price() << endl;
}
类的定义、内联函数
#include<iostream>
#include<string>
using namespace std;
class Screen {
public:
typedef std::string::size_type index;
Screen(index ht=0, index wd=0)
:contents(ht*wd,'A'),cursor(0),height(ht),width(wd)
{}
Screen(index ht, index wd, const string& cons)
{
height = ht;
width = wd;
contents = cons;
cursor = 0;
}
char get() const;
char get(index r, index c) const
{
index row = r * width;
return contents[row + c];
}
private:
std::string contents;
index cursor;
index height, width;
};
inline char Screen::get() const
{
return contents[cursor];
}
int main() {
Screen a(10, 100);
cout << a.get() << endl;
cout << a.get(2, 4) << endl;
Screen b(3, 6, "hello world to be happy");
cout << b.get() << endl;
cout << b.get(1, 2) << endl;
}
this指针的使用
#include <iostream>
#include <string>
using namespace std;
class Screen
{
public:
typedef std::string::size_type index;
Screen( index ht=0, index wd=0, const string cons= (3*5,"A"))
{
cursor = 0;
height = ht;
width = wd;
contents = cons;
}
char get() const { return contents[cursor]; }
char get(index r, index c) const { return contents[r * width + c]; }
void set(char c) { contents[cursor] = c; }
void set(index r,index c, const char ch) { contents[cursor+r*width+c] = ch; }
Screen& move(index r, index c);
private:
string contents;
index cursor;
index height, width;
};
Screen& Screen::move(index r, index c)
{
cursor = r * width + c;
return *this;
}
int main()
{
Screen myscreen(5, 3, "AAAAAAAAAAAAAAA");
cout << myscreen.get() << endl;
cout << endl;
myscreen.set(2, 3, 'E');
cout << myscreen.get(2, 3) << endl;
myscreen.move(2, 3).set('O');
cout << myscreen.get(2,3) << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class Screen
{
public:
typedef std::string::size_type index;
Screen( index ht=0, index wd=0, const string cons= (3*5,"A"))
{
cursor = 0;
height = ht;
width = wd;
contents = cons;
}
char get() const { return contents[cursor]; }
char get(index r, index c) const { return contents[r * width + c]; }
Screen& set(char c) { contents[cursor] = c; return *this; }
Screen& set(index r, index c, const char ch) { contents[cursor + r * width + c] = ch; return *this; }
Screen& move(index r, index c);
const Screen& display(ostream &om) const
{
cout << "const" << endl;
do_display(om);
return *this;
}
Screen& display(ostream& om)
{
cout << "no const" << endl;
do_display(om);
return *this;
}
private:
string contents;
index cursor;
index height, width;
void do_display(std::ostream &om) const
{
om << contents;
}
};
Screen& Screen::move(index r, index c)
{
cursor = r * width + c;
return *this;
}
int main()
{
Screen myscreen(5, 3, "AAAAAAAAAAAAAAA");
cout << myscreen.get() << endl;
cout << endl;
myscreen.set(2, 3, 'E');
cout << myscreen.get(2, 3) << endl;
myscreen.move(2, 3).set('O');
cout << myscreen.get(2,3) << endl;
myscreen.move(4, 1).set('Q').move(4, 2).set('P').move(2,2 ).set('I').display(cout).set('E').display(cout);
return 0;
构造函数
初始化列表的一些注意事项
cin初始化对象
#include <iostream>
#include <string>
using namespace std;
class Sales_items
{
public:
Sales_items(const string &book):isbn(book),units_sold(0),revenue(0){}
Sales_items():isbn(10,'A'),units_sold(0),revenue(10){}
Sales_items(istream& is)
{
cout << "Please input isbn, units, price";
is >> *this;
}
friend std::istream& operator>>(std::istream& in, Sales_items& s);
void display(ostream& co)
{
do_display(co);
}
private:
string isbn;
unsigned units_sold;
double revenue;
void do_display(ostream& co)
{
co<< isbn << units_sold << revenue << endl;
}
};
inline std::istream& operator>>(istream& in, Sales_items& s)
{
double price;
in >> s.isbn >> s.units_sold >> price;
if (in)
s.revenue = s.units_sold * price;
else
s = Sales_items();
return in;
}
int main()
{
Sales_items item1(cin);
cout << "OK! We finished !" << endl;
item1.display(cout);
return 0;
}
友元
#include <iostream>
#include <string>
using namespace std;
class Screen;
class Dog
{
public:
void foo(Screen& s);
};
class Screen
{
public:
typedef std::string::size_type index;
Screen(int ht = 0, int wd = 0) : contents(ht* wd, ' '), cursor(0), height(ht), width(wd) { cout << "Construted!"; }
int area() const
{
return height * width;
}
friend int calArea(Screen& screen);
friend class Window_Mgr;
friend void Dog::foo(Screen & s);
private:
std::string contents;
index cursor;
int height, width;
};
int calArea(Screen& screen)
{
return screen.height * screen.width;
}
class Window_Mgr
{
public:
void relocate(int r, int c, Screen& s)
{
s.height += r;
s.width += c;
}
};
void Dog::foo(Screen& s)
{
cout << s.height << "," << s.width << endl;
}
int main()
{
Screen s(10,20);
cout << endl;
cout <<"Member function " << s.area() << endl;
cout << "Friend function " << calArea(s) << endl;
cout << "Window_Mgr come!" << endl;
Window_Mgr w;
w.relocate(5, 10, s);
cout << "Member function " << s.area() << endl;
cout << "Friend function " << calArea(s) << endl;
Dog d;
d.foo(s);
return 0;
}
static 类成员
#include<iostream>
#include<string>
using namespace std;
class Account
{
public:
Account(string name, double money):
owner(name), amount(money){}
double getAmount() const
{
return this->amount;
}
static double rate()
{
return interestRate;
}
static void rate(double newRate)
{
interestRate = newRate;
}
private:
string owner;
double amount;
static double interestRate;
static const int period = 30;
};
double Account::interestRate = 0.015;
class newAccount
{
private:
static double insterestRate;
};
int main()
{
Account::rate(0.2);
Account a("Lily", 50);
cout << a.rate() << endl;
Account b("Bob", 60);
b.rate(0.5);
cout << a.rate() << endl;
return 0;
}
复制构造函数和赋值函数
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Sales_item {
public:
Sales_item():units_sold(0),revenue(0){}
Sales_item(const string &isbn_eg):isbn(isbn_eg),units_sold(0),revenue(0){}
Sales_item(const Sales_item &orig)
:isbn(orig.isbn),units_sold(orig.units_sold),revenue(orig.revenue)
{
cout << "copy construct function is used ." << endl;
}
Sales_item& operator=(const Sales_item& rhs)
{
cout << "operator= is used." << endl;
isbn = rhs.isbn;
units_sold = rhs.units_sold;
revenue = rhs.revenue;
return *this;
}
private:
string isbn;
unsigned units_sold;
double revenue;
};
int main()
{
Sales_item a;
Sales_item b("001");
Sales_item c(b);
a = b;
return 0;
}
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Sales_item {
public:
Sales_item() :units_sold(0), revenue(0)
{
cout << "default construct function is used." << endl;
}
Sales_item(const string& isbn_eg)
:isbn(isbn_eg), units_sold(0), revenue(0)
{
cout << "the 2nd construct function is used ." << endl;
}
Sales_item(const Sales_item& orig)
:isbn(orig.isbn), units_sold(orig.units_sold), revenue(orig.revenue)
{
cout << "copy construct function is used ." << endl;
}
Sales_item& operator=(const Sales_item& rhs)
{
cout << "operator= is used." << endl;
isbn = rhs.isbn;
units_sold = rhs.units_sold;
revenue = rhs.revenue;
return *this;
}
private:
string isbn;
unsigned units_sold;
double revenue;
};
Sales_item foo(Sales_item item)
{
Sales_item temp;
temp = item;
return temp;
}
int main()
{
Sales_item a;
Sales_item b("001");
Sales_item c(b);
a = b;
Sales_item item = string("003");
cout << "use function foo(): " << endl;
Sales_item ret;
ret = foo(item);
return 0;
}
类成员有指针时的复制构造函数和赋值函数
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Sales_item {
public:
Sales_item() :units_sold(0), revenue(0)
{
cout << "default construct function is used." << endl;
}
Sales_item(const string& isbn_eg)
:isbn(isbn_eg), units_sold(0), revenue(0)
{
cout << "the 2nd construct function is used ." << endl;
}
Sales_item(const Sales_item& orig)
:isbn(orig.isbn), units_sold(orig.units_sold), revenue(orig.revenue)
{
cout << "copy construct function is used ." << endl;
}
Sales_item& operator=(const Sales_item& rhs)
{
cout << "operator= is used." << endl;
isbn = rhs.isbn;
units_sold = rhs.units_sold;
revenue = rhs.revenue;
return *this;
}
private:
string isbn;
unsigned units_sold;
double revenue;
};
Sales_item foo(Sales_item item)
{
Sales_item temp;
temp = item;
return temp;
}
class NoName
{
public:
NoName():pstring(new std::string),i(0),d(0){}
NoName(const NoName& other)
:pstring(new std::string(*(other.pstring))),
i(other.i),
d(other.d)
{}
NoName& operator=(const NoName& rhs)
{
pstring = new std::string;
*pstring = *rhs.pstring;
i = rhs.i;
d = rhs.d;
}
private:
std::string* pstring;
int i;
double d;
};
int main()
{
Sales_item a;
Sales_item b("001");
Sales_item c(b);
a = b;
Sales_item item = string("003");
cout << "use function foo(): " << endl;
Sales_item ret;
ret = foo(item);
cout << endl;
cout << "Vector :" << endl;
vector<Sales_item> vec(5);
cout << endl;
cout << "数组" << endl;
Sales_item arr[] =
{ string("001"),string("002"),string("003") };
return 0;
}
|