您的位置:首页 > 编程语言 > C语言/C++

C++之从头开始(2)

2013-11-22 16:13 309 查看
1、write():输出指定字数的字符串。
  basic_ostream& write(const char_type* s, streamsize n);
  basic_ostream& write(const char_type* s, streamsize n);
  1、write遇到空字符时不会停止
  2、即使超出边界,write仍继续打印
  3、可用于数据数据——需将数值数据强制转换为char*
#include <cstdlib>

#include <iostream>

using namespace std;

int main(int argc, char *argv[])

{

const char *state1="florida";

const char *state2 = "kansas";

const char *state3 = "Erphoria";

int len = strlen(state2);

int i ;

for (i=1;i<=len;i++)

{

cout.write(state2,i);

cout<<endl;

}



for (i=len;i>0;i--)

cout.write(state2,i)<<endl;



cout.write(state2, len + 5) << endl;

long val=560031841;

cout.write((char*)&val,sizeof(long))<<endl;

system("PAUSE");

return EXIT_SUCCESS;

}

2、dec()、hex()、oct()
 计数制函数:十进制、十六进制、八进制
#include <cstdlib>

#include <iostream>

using namespace std;

int main(int argc, char *argv[])

{

int n ;

cin>>n;//默认10进制

cout<<"n n*n"<<endl;

cout<<n<<n*n<<"(decimal)\n";



cout<<hex;//16进制

cout<<n<<endl;

cout<<n*n<<"(hexadecimal)\n";



cout<<oct;//8进制

cout<<n<<""<<n*n<<endl;



hex(cout);//设置16进制

cout << n << " " << n * n << " (hexadecimal)\n";

system("PAUSE");

return EXIT_SUCCESS;

}

3、width()、fill(): 调整字宽、填充字符

iomanip设置格式:iomanip中3个常用的控制符:setprecision()、setfill()、setw(),分别用于设置精度、填充字符、和字段宽。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: