![]() |
#2
yangfrancis2016-01-05 12:21
|
如:
double prices[5]{4.99,10.98,6.87,7.99,8.48};
for (double x:prices)
cout<<x<<std::endl;
for (double x:prices)
cout<<x<<std::endl;
其中,x最初表示数组prices的第一个元素。显示第一个元素后,不断执行循环,x依次表示数组的其他元素。
要修改数组的元素时,使用语法:
for (double &x: prices)
x=x*0.80;
还可结合使用基于范围的for循环和初始化列表:
for (int x:{3,5,2,8,9})
cout<<x<<" ";
cout<<endl;
cout<<x<<" ";
cout<<endl;
这种循环主要用于各种模版容器类。
2.常见的字符输入做法:

char ch;
cin.get(ch);
while(cin.fail()==false)
{
...//do stuff
cin.get(ch);//attemp to read another char
}
//可以在上述代码中使用一些简捷方式,!算符可将true切换为false,或将false切换为true,可以使用此算符将上述while测试改写成:
while(!cin.fail())//while input has not failed
//方法cin.get(char)的返回值是一个cin对象.然而,istream类提供了一个可以将istream对象(如cin)转换为bool值的函数。
//当cin出现在需要bool值的地方(如循环的没条件测试中),时文该转换函数将被调用,另外,如果一次读取成功了,
//没转换得到的bool值为true,否则为false,所以,上述循环测试可以写为这样:
while(cin)//while input is successful
//最后,由于cin,get(char)的返回值为cin,因此可以将循环精简为:
while(cin.get(ch))//while input is successful
{
...//do stuff
}//这样,cin.get(char)只被调用一次,而不是两次:循环前一次,循环结束后一次。
cin.get(ch);
while(cin.fail()==false)
{
...//do stuff
cin.get(ch);//attemp to read another char
}
//可以在上述代码中使用一些简捷方式,!算符可将true切换为false,或将false切换为true,可以使用此算符将上述while测试改写成:
while(!cin.fail())//while input has not failed
//方法cin.get(char)的返回值是一个cin对象.然而,istream类提供了一个可以将istream对象(如cin)转换为bool值的函数。
//当cin出现在需要bool值的地方(如循环的没条件测试中),时文该转换函数将被调用,另外,如果一次读取成功了,
//没转换得到的bool值为true,否则为false,所以,上述循环测试可以写为这样:
while(cin)//while input is successful
//最后,由于cin,get(char)的返回值为cin,因此可以将循环精简为:
while(cin.get(ch))//while input is successful
{
...//do stuff
}//这样,cin.get(char)只被调用一次,而不是两次:循环前一次,循环结束后一次。
3.写入到文本文件

//必须包含头文件fstream。
//头文件fstream定义了一个用于处理输出的ofstream类(cout便是头文件iostream预定义(声明)好的一个名为cout的ofstream对象)。
//需要声明一个或多个ofstream对象,并命名。
//必须指明名称空间。比如为使用ofstream类,必须使用编译指令using,或前缀std::。
//需要将自己定义好的ofstreamc对象与文件关联起来,方法之一是使用open()方法。
//使用完文件后,应用方法close()奖其关闭
//声明一个ofstream对象并将其同文件关联后,便可以像使用cout那样使用它。
#include <iostream>
#include <fstream>
int main()
{
using namespace std;
ofstream outFile;//fostream对象的定义
outFile.open("fish.txt");//关联文件
for(int i=0;i<10;i++)
{
outFile<<i<<"\t";
}
outFile<<endl<<"Done.";
outFile.close();//关闭文件
return 0;
}
//或者像这样:
#include <iostream>
#include <fstream>
int main()
{
//using namespace std;
std::ofstream outFile;//fostream对象的定义
outFile.open("fish.txt");//关联文件
for(int i=0;i<10;i++)
{
outFile<<i*2<<"\t";
}
outFile<<std::endl<<"Done.";
outFile.close();
return 0;
}
//头文件fstream定义了一个用于处理输出的ofstream类(cout便是头文件iostream预定义(声明)好的一个名为cout的ofstream对象)。
//需要声明一个或多个ofstream对象,并命名。
//必须指明名称空间。比如为使用ofstream类,必须使用编译指令using,或前缀std::。
//需要将自己定义好的ofstreamc对象与文件关联起来,方法之一是使用open()方法。
//使用完文件后,应用方法close()奖其关闭
//声明一个ofstream对象并将其同文件关联后,便可以像使用cout那样使用它。
#include <iostream>
#include <fstream>
int main()
{
using namespace std;
ofstream outFile;//fostream对象的定义
outFile.open("fish.txt");//关联文件
for(int i=0;i<10;i++)
{
outFile<<i<<"\t";
}
outFile<<endl<<"Done.";
outFile.close();//关闭文件
return 0;
}
//或者像这样:
#include <iostream>
#include <fstream>
int main()
{
//using namespace std;
std::ofstream outFile;//fostream对象的定义
outFile.open("fish.txt");//关联文件
for(int i=0;i<10;i++)
{
outFile<<i*2<<"\t";
}
outFile<<std::endl<<"Done.";
outFile.close();
return 0;
}
4.读取文本文件

///必须包含头文件fstream.
///头文件stream定义了了用于处理输入的类ifstream
///需要声明一个或多个ifstream对象,并命名
///必须指明名称空间。比如为使用ifstream类,必须使用编译指令using,或前缀std::。
///需要将自己定义好的ifstreamc对象与文件关联起来,方法之一是使用open()方法。
///使用完文件后,应用方法close()奖其关闭
///可结合使用ifstream对象和get()方法来读取一个字符,使用ifstream对象和getline()读取一行字符。
///可结合使用ifstream对象和eof(),fail()等方法来判断输入是否成功
///ifstream对象本身被用作测试条件时,如果最后一个读取操作成功,它将被转换为bool型值true,否则为false.
///声明一个ifstream对象并将其同文件关联后,便可以像使用cin那样使用它。
/**声明对象与关联文件的方法
ifstream inFile;
ifstream fin;//对象的定义
inFile.open("bowling.txt");
char filename[50];
cin>>filename;
fin.open(filename);//文件的关联
double wt;
inFile>>wt;//reading a number from bowling.txt
char line[81];
fin.getline(line,81);//read a line of text
//对象的使用
///检测文件是否成功打开的方法:
inFile.open("bowling.txt");
if(!inFile.if_open())//如果文件成功打开,is_open()返回true。
{
exit(EXIT_FAILURE)//exit()被定义于头文件cstdlib中,作用是终止程序
}
**/#include<iostream>
#include<fstream>
#include<cstdlib>
const int SIZE=60;
int main()
{
using namespace std;
char filename[SIZE];
ifstream inFile; // object for handling file input
cout << "Enter name of data file: ";
cin.getline(filename, SIZE);
inFile.open(filename); // associate inFile with a file
if (!inFile.is_open()) // failed to open file
{
cout << "Could not open the file " << filename << endl;
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
}
double value;
double sum = 0.0;
int count = 0; // number of items read
inFile >> value; // get first value
while (inFile.good()) // while input good and not at EOF
{
++count; // one more item read
sum += value; // calculate running total
inFile >> value; // get next value
}
if (inFile.eof())
cout << "End of file reached.\n";
else if (inFile.fail())
cout << "Input terminated by data mismatch.\n";
else
cout << "Input terminated for unknown reason.\n";
if (count == 0)
cout << "No data processed.\n";
else
{
cout << "Items read: " << count << endl;
cout << "Sum: " << sum << endl;
cout << "Average: " << sum / count << endl;
}
inFile.close(); // finished with the file
return 0;
}
///头文件stream定义了了用于处理输入的类ifstream
///需要声明一个或多个ifstream对象,并命名
///必须指明名称空间。比如为使用ifstream类,必须使用编译指令using,或前缀std::。
///需要将自己定义好的ifstreamc对象与文件关联起来,方法之一是使用open()方法。
///使用完文件后,应用方法close()奖其关闭
///可结合使用ifstream对象和get()方法来读取一个字符,使用ifstream对象和getline()读取一行字符。
///可结合使用ifstream对象和eof(),fail()等方法来判断输入是否成功
///ifstream对象本身被用作测试条件时,如果最后一个读取操作成功,它将被转换为bool型值true,否则为false.
///声明一个ifstream对象并将其同文件关联后,便可以像使用cin那样使用它。
/**声明对象与关联文件的方法
ifstream inFile;
ifstream fin;//对象的定义
inFile.open("bowling.txt");
char filename[50];
cin>>filename;
fin.open(filename);//文件的关联
double wt;
inFile>>wt;//reading a number from bowling.txt
char line[81];
fin.getline(line,81);//read a line of text
//对象的使用
///检测文件是否成功打开的方法:
inFile.open("bowling.txt");
if(!inFile.if_open())//如果文件成功打开,is_open()返回true。
{
exit(EXIT_FAILURE)//exit()被定义于头文件cstdlib中,作用是终止程序
}
**/#include<iostream>
#include<fstream>
#include<cstdlib>
const int SIZE=60;
int main()
{
using namespace std;
char filename[SIZE];
ifstream inFile; // object for handling file input
cout << "Enter name of data file: ";
cin.getline(filename, SIZE);
inFile.open(filename); // associate inFile with a file
if (!inFile.is_open()) // failed to open file
{
cout << "Could not open the file " << filename << endl;
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
}
double value;
double sum = 0.0;
int count = 0; // number of items read
inFile >> value; // get first value
while (inFile.good()) // while input good and not at EOF
{
++count; // one more item read
sum += value; // calculate running total
inFile >> value; // get next value
}
if (inFile.eof())
cout << "End of file reached.\n";
else if (inFile.fail())
cout << "Input terminated by data mismatch.\n";
else
cout << "Input terminated for unknown reason.\n";
if (count == 0)
cout << "No data processed.\n";
else
{
cout << "Items read: " << count << endl;
cout << "Sum: " << sum << endl;
cout << "Average: " << sum / count << endl;
}
inFile.close(); // finished with the file
return 0;
}
txt文件自己命名,内容为:
18 19 18.5 13.5 14
16 19.5 20 18 12 18.5
17.5
/////////17.5后面最好有个回车,不然可能不会读取17.5
5.const关键字:
void show_array(const double ar[],int n)
const只表明此调用将ar[]看作只读数据,不能在此调用中改变其值,并不表明ar[]是常量数组。
int age=39;
const int *pt=&age;//声明pt指向一个const int,因此不能用pt来修改这个值,也就是说*pt是一个const值,不能被修改,对pt而言,其指向值为常量,但被指向的量不一定为常量,如此处,age不是常量,可以改变age的值,但不能通过改变*pt来改变age的值
6.函数指针

#include<iostream>
double add(double x,double y){return x+y;}//加法
double multi(double x,double y){return x*y;}//乘法,两个函数的共同点是参数列表一致
double calculate(double m,double n,double (*pf)(double p,double q)){return (*pf)(m,n);}//计算函数,传递两个待计算的数,并用指向函数的指针调用计算函数
int main()
{
typedef double (*pFunc)(double,double);//定义指针函数
pFunc pa[2]{add,multi};//指向函数的指针数组并初始化为相关函数地址
std::cout<<"Enter two numbers: ";
double m,n;
while(std::cin>>m>>n)
{
std::cout<<"add:"<<calculate(m,n,pa[0])<<std::endl;//做加法
std::cout<<"multi:"<<calculate(m,n,pa[1])<<std::endl;//做乘法
}
std::cout<<"Down!\n";
return 0;
}//可见指向函数的指针还是有一定优势的,能减小代码量,简化写法
double add(double x,double y){return x+y;}//加法
double multi(double x,double y){return x*y;}//乘法,两个函数的共同点是参数列表一致
double calculate(double m,double n,double (*pf)(double p,double q)){return (*pf)(m,n);}//计算函数,传递两个待计算的数,并用指向函数的指针调用计算函数
int main()
{
typedef double (*pFunc)(double,double);//定义指针函数
pFunc pa[2]{add,multi};//指向函数的指针数组并初始化为相关函数地址
std::cout<<"Enter two numbers: ";
double m,n;
while(std::cin>>m>>n)
{
std::cout<<"add:"<<calculate(m,n,pa[0])<<std::endl;//做加法
std::cout<<"multi:"<<calculate(m,n,pa[1])<<std::endl;//做乘法
}
std::cout<<"Down!\n";
return 0;
}//可见指向函数的指针还是有一定优势的,能减小代码量,简化写法
[此贴子已经被作者于2016-1-15 11:24编辑过]