64,691
社区成员
发帖
与我相关
我的任务
分享
#include <cstdlib>
#include <vector>
#include <iostream>
using std::vector;
using std::cout;
using std::endl;
using std::ios;
void calculateAdjClosePrice(double *close, double *open, double *adj_close);
int main(int argc, char** argv) {
vector<double> close;
vector<double> open;
vector<double> adjclose;
close.push_back(2.02);
open.push_back(1.52);
adjclose.push_back(1);
calculateAdjClosePrice(close,open,adjclose);
return 0;
}
void calculateAdjClosePrice(vector<double> close, vector<double> open, vector<double> adj_close){
int size=close.size();
adj_close.clear();
for(int i=0;i<size;i++){
if (close.at(i)>open.at(i))
adj_close.push_back(close.at(i)+0.01*open.at(i));
else
adj_close.push_back(close.at(i)-0.01*open.at(i));
}
vector<double>::iterator p;
for (p=adj_close.begin();p!=adj_close.end();p++)
cout<<*p<<endl;
}
#include <cstdlib>
#include <cstring>
#include <string>
#include<iostream>
using std::cout;
using std::cerr;
using std::endl;
template <class T>
void strswap(T* a, T* b);
int main(int argc, char** argv) {
char *A="I am a graduate student";
char *B="You are a financial man";
strswap(A,B);
cout<<"A = "<<A<<endl;
cout<<"B = "<<B<<endl;
return 0;
}
template <class T>
void strswap(T* a, T* b){
if (strlen(a)!=strlen(b))
cerr<<"Error: The length of the string should be matched."<<endl;
else{
int len=strlen(a);
T *c=new T [len+1];
for (int i=0;i<len;i++){
c[i]=a[i];
a[i]=b[i];
b[i]=c[i];
}
delete []c;
}
}
我先定义了函数形式:void strswap(T* a, T* b);
最后再定义其具体形式。
这样定义函数在这个例子中是可行的,为什么在vector的例子中就不可行了呢?