#include <iostream>
#include <string>
#include<fstream>
#include"headd.h"
#include"sourse2.cpp"
#include<sstream>
#include <vector>
#include<list>
using std::cout;
using std::cin;
using std::endl;
using namespace std;//import所有的std
int main(){
//刷新缓冲区的三办法
cout << "sdf" << endl;//换行
cout << "sd" << std::flush;//不添加任何字符
cout << "df!" << std::ends;//这个添加一个空字符
cout << unitbuf;//告诉流每次输出操作后都刷新缓冲区
cout << nounitbuf;//恢复正常的缓冲方式
//IO类
fstream outfile;
outfile.open("ds .txt");
fstream fstrm;//看一下怎么用这些fstream类
fstrm.open("sd");//看后面
fstream fstrm("dead");
fstrm.is_open;//fstream的特有操作看一下
Screen as;//是不是不管定义在哪里 include进来就得 zhiye guifan ne
ifstream input("dsd");
ofstream output("ed");
Sales_data total;
if (read(input, total)) {
Sales_data trans;
while (read(input,trans))
{
if (total.isdn() == trans.isdn())
total.combine(trans);//新的加进来
else
{
print(output, total) << endl;//打印当前的总量然后在令等 等了之后满足上头后正好求和
total = trans;
}
}
print(output, total) << endl;
}
else
cerr << "no data!?" << endl;
ifstream in("wdew");//打开指定文件
ofstream out;
string ifile = "C:/Users/Administrator/Desktop/新建文本文档.txt";
out.open(ifile+ ".copy");//打开指定文件
if (out) {//检查是否打开成功 好习惯
return 0;
}
in.close();//关闭文件 因为我们一旦用文件流打开了,就会保持与文件的关联,要打开关联另一个就先close这个
in.open(ifile + "2");//打开另一个文件
//文件模式
in.open("ed");//以读方式打开
out.open("dsc");//以写方式打开
ifstream in("asd");//两种方式打开
ifstream in;
in.open("C:/Users/Administrator/Desktop/新建文本文档.txt");
fstream out("dsfa");
fstream out;
out.open("asdfa");
//ifstream和fstream匹配in模式,ofstream和fstream匹配out模式ok
//以out模式打开文件内容会丢弃
ofstream out("dfsf");//这三个都会截断
ofstream out1("eesdf", ofstream::out);
ofstream out2("sdf", ofstream::out | ofstream::trunc);
//为了保留文件内容,那么请显式app
ofstream app("sfd", ofstream::app);
ofstream app2("asd", ofstream::out | ofstream::app);
//我们每次打开文件时都可以改变文件模式
ofstream out;
out.open("dsf");
out.close();
out.open("ad", ofstream::app);
out.close();
struct PersonInfo
{
string name;
vector<string> phones;
};
string line, word;
vector<PersonInfo>people;
while (getline(cin,line))
{
PersonInfo info;
istringstream record(line);//把记录绑定到刚读入的行(说白了这个istringstream就是可以从string里读取字符 读成一个个字符)一句话读成单词
record >> info.name;
while (record>>word)//一旦录入成功 那么
{
info.phones.push_back(word);//录入下一条
}
people.push_back(info);//把记录加到people(总的记录)
}
int *p = new int;
delete p;
//动态内存
shared_ptr<string> p1;//没初始化所以空指针
shared_ptr<string>p3;
shared_ptr<list<int>> p2;
if (!p1)
cout << "empty" << endl;
p1.swap(p3);
bool a = p1.unique();//是否独一
int t = p1.use_count();//返回与这指针共享对象的指针数目 用于调试
//share_ptr用make_shared来初始化(直接初始化)
shared_ptr<int> p4 = make_shared<int>(22);
shared_ptr<string>p5 = make_shared<string>(11, '2');
shared_ptr<int>p6 = make_shared<int>();//这个int被值初始化 所以p6指向0
if (p6)
cout << "0" << endl;
auto p = make_shared<int>(42);
auto q(p);
auto r = make_shared<int>(23);
r = q;//r指向另一个地址 q指向的对象引用+,r原来指向的引用-,如果减到没有引用者就释放了(销毁)
// 直接管理内存 使用new分配内存
string *ps1 = new string;//默认初始化为空string
string *ps2 = new string();//有括号就值初始化 也是空string
int *ps3 = new int;//默认初始化 指针值未定义
int *ps4 = new int();//值初始化为0,*ps4为0
int *ps = new int(1024);
string *ps5 = new string(12, 'a');//这两个为直接初始化
auto p1 = new auto("edwe");
auto p2 = new auto(233);//当初始化器为单值的时候可以auto
int s = 1;
int *p3 = new (nothrow) int;//如果分配失败就返回一个空指针
delete ps3;//注意这个曾经的bug delete必须放在函数体内才行
//一旦一个被释放 那么另一个指向他的指针也就被释放了 你再释放他就报错辣
//unique_ptr的初始化
unique_ptr<double> p10;
unique_ptr<int>p11(new int(234));
//因为unique_ptr拥有它的对象,所以不支持拷贝和赋值(不要通过这两种途径去初始化了)
//同一时刻只能有一个unique_ptr指向指定对象
unique_ptr<string> p12(new string());//我试一下值初始化
unique_ptr<string> p13(p12.release());//初始化直接转移接盘
unique_ptr<string>p14(new string("sse"));
p13.reset(p14.release());//把p13原来指向的释放掉,然后把p14的转移给它
//weak_ptr的初始化
weak_ptr<int>wp = make_shared<int>(42);//weak_ptr用make_shared来初始化
//弱weak不改变引用次数
if (shared_ptr<int> np = wp.lock()) {//只要weak对象还存在 那么lock就返回指向共享的指针
return 0;
}
//动态数组 使用new (这两个只定义还未初始化)
int *pia = new int[42];//方括号内分配数量
typedef int arrv[43];
int *y = new arrv;//用类型别名 等效
//初始化动态数组
int *y1 = new int[10]();//值初始化了 全为0
string *y2 = new string[12]();//值初始化了 为空string
int *y3 = new int[10]{ 1,1,1,1,11,1,1 };//不够的自己值初始化
string *y4 = new string[10]{ "a","s","2","we",string(3,'x') };//初始化器只能少不能多哈
delete [] y1;//你在释放动态数组的时候必须加[]
unique_ptr<int[]> up(new int[10]);//未初始化
up.release();//自动用delete销毁
allocator<string> alloc;
auto const p = alloc.allocate(3);//给他分配3个未初始化的string(内存)
//allocator分配的内存未构造 下面是构造
auto cc = p;//当然cc是个指针型
alloc.construct(cc++);//构造
alloc.construct(cc++, 10, 'c');
alloc.construct(cc++, "hi");
system("pause");
return 0;
}
class strBlob { //
public:
typedef vector<string>::size_type size_type;
strBlob();
strBlob(initializer_list<string> i1);
size_type size() const { return data->size(); }
bool empty()const { return data->empty(); }
void push_back(const string &t) { data->push_back(t);}
void pop_back();
string& front();
string& back();
private:
shared_ptr<vector<string>> data;//类只有一个数据成员
void check(size_type i, const string &msg)const;
};
strBlob::strBlob():data(make_shared<vector<string>>()){}
strBlob::strBlob(initializer_list<string>i1):data(make_shared<vector<string>>(i1)){}
void strBlob::check(size_type i, const string &msg)const
{
if (i >= data->size())
throw out_of_range(msg);
}
string& strBlob::front() {
check(0, "front on empty strBlob");
return data->front();
}
string& strBlob::back() {
check(0, "back on empty strBlob");
return data->back();
}
void strBlob::pop_back() {
check(0, "pop_back on empty strBlob");
data->pop_back();
}
//直接管理内存 new and delete
//new只能返回一个指针
C++_programming_basis_2(IO类,动态内存等)
猜你喜欢
转载自blog.csdn.net/puying1/article/details/83713915
今日推荐
周排行