2.4
//:C02:Stream2.cpp
#include<iostream>
using namespace std;
int main()
{
cout<<"decimal:"<<dec<<15<<endl;
cout<<"octal:"<<oct<<15<<endl;
cout<<"hex:"<<hex<<15<<endl;
cout<<"a floating-point number:"<<3.14159<<endl;
cout<<"non-printing char (escape):"<<char(27)<<endl;
}///:~
2.4.1字符数组的拼接
//:C02:Concat.cpp
#include<iostream>
using namespace std;
int main()
{
cout<<"hua "
"guang "
"lu "
"\n";
}///:~
2.4.2读取输入数据
//:C02:Numconv.cpp
#include<iostream>
using namespace std;
int main()
{
int number;
cout<<"Enter a decimal number: ";
cin>>number;
cout<<"octal: "<<oct<<number<<endl;
cout<<"hex: "<<hex<<number<<endl;
}///:~
2.4.3调用其他程序----system( )
//:C02:CallHello.cpp
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
system("D:\\kankan\\再爱我一次.xv");
}///:~
2.5字符串简介
//:C02:HelloStrings.cpp
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s1,s2;
string s3="hello,world";
string s4("I am ");
s2="Today";
s1=s3+" "+s4;
s1 += " 8 ";
cout<<s1+s2+"!"<<endl;
}///:~
2.6 文件的读写
//:C02:Scopy.cpp
#include<string>
#include<fstream>
using namespace std;
int main()
{
ifstream in("Scopy.cpp");
ofstream out("Scopy2.cpp");
string s;
while (getline(in,s))
{
out<<s<<"\n";
}
}///:~
2.6.1把整个文件拷贝成一个单独的string对象:
//:C02:FillString.cpp
#include<string>
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
ifstream in("FillString.cpp");
string s,line;
while (getline(in,line))
{
s += line+"\n";
}
cout<<s;
}///:~
2.7 vector 简介
2.7.1vector 一次读一行
//:C02:FillVector.cpp
#include<string>
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
int main()
{
vector<string> v;
ifstream in("FillString.cpp");
string line;
while (getline(in,line))
{
v.push_back(line);
}
for (int i=0;i<v.size();i++)
{
cout<<v[i]<<endl;
}
}///:~
2.7.2vector一次读一个单词
//:C02:GetWords.cpp
#include<string>
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
int main()
{
vector<string> v;
ifstream in("FillString.cpp");
string word;
while (in>>word)
{
v.push_back(word);
}
for (int i=0;i<v.size();i++)
{
cout<<v[i]<<" ";
}
}///:~
2.7.3 创建vector<int>的例子
//:C02:IntVector.cpp
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v;
for (int i=0;i<10;i++)
{
v.push_back(i);
}
for ( i=0;i<v.size();i++)
{
cout<<v[i]<<" , ";
}
cout<<endl;
for ( i=0;i<v.size();i++)
{
v[i] *= 10;
}
for ( i=0;i<v.size();i++)
{
cout<<v[i]<<" , ";
}
cout<<endl;
}///:~