目前,我的代码就是这样:
void ReadFile(double Cst[][1000], char* FileName, int height)
FILE* ifp;
double value;
int nRead = 0;
int mRead = 0;
//open the file, check if successful
ifp = fopen( FileName, "r" );
if (ifp==NULL){
...
}
for (nRead = 0; nRead < height; nRead++){
for (mRead = 0; mRead < 1000; mRead++){
fscanf(ifp, "%le",&value);
Cst[nRead][mRead]=value;
}
}
fclose(ifp);
Run Code Online (Sandbox Code Playgroud)
我可以改变什么来使它尽可能快?
我想循环遍历向量并擦除对应于特定条件的某些元素,例如:
vector<int> myvector;
vector<int>::iterator it;
myvector.push_back(1);
myvector.push_back(2);
myvector.push_back(3);
myvector.push_back(4);
for(it = myvector.begin(); it != myvector.end(); ++it){
if((*it) == 4){
it = myvector.erase(it);
}
}
Run Code Online (Sandbox Code Playgroud)
现在这个工作正常,除非标准删除了上面代码中的最后一项.你怎么避免这种行为?
谢谢.
编辑 - - - - - - - - - - - - - - - - - -
现在我循环它的原因是我需要从中删除元素实际上有4个向量(但标准仅在一个向量上):
在这种情况下,这是怎么走的?
vector<int> myvector;
vector<int> myvector2;
vector<int> myvector3;
vector<int> myvector4;
vector<int>::iterator it;
vector<int>::iterator it2;
vector<int>::iterator it3;
vector<int>::iterator it4;
myvector.push_back(1);
myvector.push_back(2);
myvector.push_back(3);
myvector.push_back(4);
Run Code Online (Sandbox Code Playgroud)
(假设myvector2/3/4中有值)
it2 = myvector2.begin()
it3 = myvector3.begin()
it4 = myvector4.begin()
for(it = myvector.begin(); …Run Code Online (Sandbox Code Playgroud) 这是我做的一个小测试,结果让我感到惊讶:做两次相同的循环大约是循环一次的两倍.我猜它是因为内存访问?
float* A = new float[1000000];
float* B = new float[1000000];
int h,w;
h = w = 1000;
CString txt;
double time1, time2;
time1 = Timer::instance()->getTime();
for(int j = 0; j < h; j++){
for(int i = 0; i < w; i++){
A[i+j*w] = 1;
B[i+j*w] = 1;
}
}
time2 = Timer::instance()->getTime();
txt.Format(_T("Both in same loop = %f"),time2-time1);
AfxMessageBox(txt);
time1 = Timer::instance()->getTime();
for(int j = 0; j < h; j++){
for(int i = 0; i < w; i++){
A[i+j*w] …Run Code Online (Sandbox Code Playgroud)