曾经,我非常羡慕那些人见人爱的人,我也想要变成那样,可是后来我才明白人见人爱也是需要天赋的,后来我开始默默努力,我想,就算我不能让每个人都喜欢我,至少因为我做的努力能得到别人的尊重。

c++之vector

一、vector基本操作  

 

  vector是动态数组,其元素可以是任意类型,比如下面是一个简单的例子,这个例子中,就会介绍到vector的创建、赋值、访问以及一些方法。

 

#include <iostream>
#include <windows.h>
#include <vector>
using namespace std;

void modify(vector< vector<int> > &ivec){
    // 在第一行的下标2处添加一个元素666
    ivec[0].insert(ivec[0].begin() + 2, 666);
    // 在第六行的下标1处删除一个元素
    ivec[5].erase(ivec[5].begin() + 1);
    // 删除最后一行
    ivec[9].clear();
}

int main() {
    vector< vector<int> > ivec;
    for (int i = 0; i < 10; i++) {
        vector<int> vec;
        for (int j = 0; j < 10; j++) {
            vec.push_back(i + j);
        }
        ivec.push_back(vec);
    }
    
    // 在第一行末尾添加一个元素
    ivec[0].push_back(999);

    // 将第二行末尾的两个元素删除
    ivec.at(1).pop_back();
    ivec.at(1).pop_back();

    // 直接访问
    for (int i = 0; i < ivec.size(); i++) {
        for (int j = 0; j < ivec[i].size(); j++) {
            cout << ivec[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;

    // 调用函数
    modify(ivec);

    // 使用指针访问
    vector< vector<int> >::iterator IE;
    vector<int>::iterator it;
    for (IE = ivec.begin(); IE != ivec.end(); IE++) {
        for (it = (*IE).begin(); it != (*IE).end(); it++) {
            cout << *it << " ";
        }
        cout << endl;
    }

    cout << endl;
    cout << ivec[0][10] << endl; // 999
    cout << ivec[0].at(10) << endl; // 999
    cout << ivec.at(0).at(10) << endl; // 999

    // 注意这里front/back和begin/end的区别,
    cout << ivec[0].front() << endl; // 即front返回的时第一行的第一个元素;而begin是迭代器,需要用*访问到元素
    cout << ivec[0].back() << endl; // 同样,back返回的时第一行的最后一个元素;而back是迭代器,需要用*访问到元素

    cout << ivec[0][1000] << endl;     //1398145138 越界!但是c++不会报错,就像过马路一样,不该过时,没人提醒你不该过,出了错自己扛。
    system("pause");
}

 

 

  最终结果如下:

0 1 2 3 4 5 6 7 8 9 999
1 2 3 4 5 6 7 8
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18

0 1 666 2 3 4 5 6 7 8 9 999
1 2 3 4 5 6 7 8
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17


9
9
9
0
999
1398145138

  即我们需要引入库文件 vector。 然后这里创建的是一个二维int型数组,对于元素,我们可以用[]访问,也可以用at()访问,但是前者在越界时不会提示。另外,vector支持迭代器,用起来会比较方便。vector与传统数组的区别在于vector不需要指定数组的打下,而是动态变化的。 另外,我们可以看到,可以使用push_back()和pop_back()方便的操作数组;使用insert()和erase()可以很容易地在任何位置添加或者删除数组; 使用begin()和end()可以获取到vector的迭代器;使用size()可以获得vector的大小。 更多详细内容可以看程序注释。

  而如果我们希望将之作为参数传入函数中调用应该怎么做呢?上述的modify就是一个例子,即我们将上面程序中两个循环输出之间的 insert和erase 封装为一个函数,然后直接调用即可,值得注意的是参数的变量名前要有 &,这样才能成功调用。 

 

 

二、vector赋初始值

  在上面的例子中,我们使用push_back给vector赋值,但是如果我们希望直接赋值呢,难道要用多个push_back吗?这显然是不合理的,所以,下面介绍几种赋值方法。

1、不带参数的构造函数初始化:

//初始化一个size为0的vector
vector<int> foo;

 

2、构造默认值的vector:

vector<int> arr(10); // 返回默认值为0,长度为10

  如果我们第二个参数也是一个数字,那么就是默认值:

vector<int> arr(10, 8); //长度为10,值全部为8

 

3、根据数组地址进行初始化:

 int a[5] = {88, 52, 36, 96, 7};
 vector<int> arr(a, a+5);

  如上,我们构建出来的vector长度为5,值和数组a的值相同。这种方法比较常用。

 

 

4、通过同类型的vector进行初始化:

vector<int> a(10, 8);
vector<int> arr(a);

如上,得到的arr和a是一样的。

 

5、使用insert初始化:

int a[6] = {6, 6, 6, 6, 6, 6};
vector<int> arr;
arr.insert(arr.begin(), a, a + 7);

 

 

  

三、vector算法

  在上面的例子中,我们对vector可以进行简单的操作,而除此之外,vector还提供了一些简单的算法供我们使用,比如vector元素的反转以及排序等等。

     在使用算法时,需要引入头文件:

#include<algorithm>

  接着,就可以使用下面的常见算法了。

 

1、reverse() - 元素反转。

   在引入algorithm库文件之后,直接使用reverse(vec.begin(), vec.end());即可实现反转功能,如下所示:

#include <iostream>
#include <algorithm>
#include <windows.h>
#include <vector>
using namespace std;
void output(vector<int> &arr) {
    vector<int>::iterator it;
    for (it = arr.begin(); it != arr.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}
int main() {
    int a[10] = {15, 85, 65, 12, 5};
    vector<int> arr(a, a+5);
    // 输出初始vector
    output(arr);
    // 反转
    reverse(arr.begin(), arr.end());
    // 输出反转之后的vector
    output(arr);
    system("pause");
}

  最终结果如下:

15 85 65 12 5
5 12 65 85 15 

  可见,通过reverse,成功实现了反转功能。 

  

2、 sort() - 元素排序

  同样,需要引入algorithm库文件,然后直接使用sort(vec.begin(), vec.end());即可实现vector的排序。如下:

#include <iostream>
#include <algorithm>
#include <windows.h>
#include <vector>
using namespace std;
void output(vector<int> &arr) {
    vector<int>::iterator it;
    for (it = arr.begin(); it != arr.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}
int main() {
    int a[10] = {15, 85, 65, 12, 5};
    vector<int> arr(a, a+5);
    // 输出初始vector
    output(arr);
    // 排序
    sort(arr.begin(), arr.end());
    // 输出排序之后的vector
    output(arr);
    system("pause");
}

  最终,实现了从小到大的排序:

15 85 65 12 5
5 12 15 65 85

  默认情况下是升序排序,如果我们希望降序排序,可以定义一个函数,如下:

#include <iostream>
#include <algorithm>
#include <windows.h>
#include <vector>
using namespace std;
void output(vector<int> &arr) {
    vector<int>::iterator it;
    for (it = arr.begin(); it != arr.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}
bool camp(int &a, int &b) {
    return a>b;
}
int main() {
    int a[10] = {15, 85, 65, 12, 5};
    vector<int> arr(a, a+5);
    // 输出初始vector
    output(arr);
    // 反转
    sort(arr.begin(), arr.end(), camp);
    // 输出反转之后的vector
    output(arr);
    system("pause");
}

  这样,最终的结果就是降序排序,如下:

15 85 65 12 5
85 65 15 12 5

 

 

四、元素是结构体的vector

  之前使用到的vector,其元素都是相同类型的,比如int、double等,但是,如果希望类型是不同的结构体呢,这时就需要用到结构体了,如下所示:

#include <iostream>
#include <windows.h>
#include <vector>
using namespace std;
struct Phi {
    int i; 
    double j;
};
int main() {
    vector<struct Phi> pvec;
    struct Phi phi;
    phi.i = 1;
    phi.j = 1.1;
    pvec.push_back(phi);

    cout << pvec[0].i << endl; // 1

    vector<struct Phi>::iterator it = pvec.begin();
    cout << it->i << endl; // 1
    cout << (*it).i << endl; // 1

    system("pause");
}

   最终结果如下:

1
1
1
  • 注意:这里struct Phi的定义必须在全局中,而不能在main()函数中,否则会报错。
  • 这里声明结构体vector时,可以是vector<struct Phi> pvec也可以是vector<Phi> pvec(定义遍历器同理),因为Phi就代表一种数据类型了,所以struct也是可以省略不写的。
  • 在获取数据时,我们可以有三种方法:即pvec[0].i,即这个结构体数组中第一个的i值;或者it->i这是用迭代器进行访问的一种方式;或者是(*it).i也是使用迭代器访问的一种方式。
  • 同样的,struct Phi phi也可以写成Phi phi,即省略strcut不写,但写了之后易读性会更好一些。

 

  上面的过程中,仅仅是对vector的基本操作,但是我为了解决实际问题:网格上的每个坐标都有一个vector,这个vector存着若干个结构体,每个结构体里存储着晶粒标号和相场值;所以,这个遍历器应当是一个二维数组用来表示特定的一个点,所以,可以是下面这样的:

#include <iostream>
#include <windows.h>
#include <vector>
using namespace std;
struct Phi {
    int i; 
    double j;
};
int main() {
    // 创建一个vector,其中元素是结构体
    vector<struct Phi> pvec;

    // 创建结构体
    Phi phi;
    phi.i = 1;
    phi.j = 0.1;
    pvec.push_back(phi); // 将一个结构体push_back到vector中

    // 这里一旦覆盖,相当于又创建了一个结构体
    phi.i = 3;
    phi.j = 0.2;
    pvec.push_back(phi); // push_back

    // 同样的,又创建了一个结构体
    phi.i = 5;
    phi.j = 0.7;
    pvec.push_back(phi);

    cout << pvec[2].j << endl;

    // 创建一个迭代器,相当于指针,这里使用foo[0][0],这样,就可以使用二维数组了。
    vector<struct Phi>::iterator foo[0][0];
    foo[0][0] = pvec.begin();
    cout << (foo[0][0] + 1)->i << endl; // 3

    system("pause");
}

  即这里的vector我多push_back进入了几个结构体,然后,这里的迭代器用的是foo[0][0],即表示(0, 0)这个坐标点的结构体是pvec,其他的坐标点同样可以这样表示。 

 

  于是,我们可以用嵌套的for循环来解决这个问题,如下:

#include <iostream>
#include <windows.h>
#include <vector>
using namespace std;
struct Phi {
    int i; 
    double j;
};
int main() {
    
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            // 创建一个vector,其中元素是结构体
            vector<struct Phi> pvec;
            // 创建一个迭代器,相当于指针,这里使用foo[i][j],实现对每一个网格点的赋值
            vector<struct Phi>::iterator foo[i][j];
            
            // 创建结构体
            Phi phi;
            phi.i = 1;
            phi.j = 0.1;
            pvec.push_back(phi); // 将一个结构体push_back到vector中
            
            // 这里一旦覆盖,相当于又创建了一个结构体
            phi.i = 3;
            phi.j = 0.2;
            pvec.push_back(phi); // push_back
            
            // 同样的,又创建了一个结构体
            phi.i = 5;
            phi.j = 0.7;
            pvec.push_back(phi);
            
            foo[i][j] = pvec.begin();
        }
    }

    // cout << foo[1][1]->i << " " << foo[1][1]->j << endl;

    system("pause");
}

  如上,每个for循环中,我们都创建一个vector,然后创建一个迭代器,接着,我们就可以将多个结构体push到这个vector中,最后,我们让迭代器指向这个vector即可。但是,vector更占据内存(nector是对数组的包装,故占用内存更多),所以,格点的数量即使不是很多,就会因为内存占用过多而崩溃,另外,因为这个指针是在for循环之内的局部变量,所以无法在for循环之外来访问,并且将之声明为全局变量时还会报错,即使声明为全局变量,内存占用较多的问题也不能忽视,特别是在项目中,如果晶粒数特别多,这种方法还是不可取的。但是这个探索还是有意义的,至少,这个思路是对的。

 

 


 

5月15日更新 - 上面的计算并不是说真的在数量大时就崩溃,而是程序有问题,下面贴上正确的程序;

#include <iostream>
#include <windows.h>
#include <vector>
using namespace std;
struct Phi {
    int m; 
    double n;
};
int main() {
    vector<struct Phi>::iterator foo[100][100];
    vector<struct Phi> pvec[100][100];
    Phi phi;
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            // 创建结构体
            phi.m = i; 
            phi.n = 0.1;
            pvec[i][j].push_back(phi); 
            
            phi.m = i;
            phi.n = 0.2;
            pvec[i][j].push_back(phi);
            
            phi.m = i;
            phi.n = 0.7;
            pvec[i][j].push_back(phi);
            
            foo[i][j] = pvec[i][j].begin(); 
        }
    }
    cout << (foo[88][88] + 1)->m << endl;
    cout << (foo[88][88] + 1)->n << endl;
    system("pause");
}

  如上所示,这个程序可以正常运行,并且在数据比较大的时候,也不会出现问题。方法: 问题的解决需要拆分,即将大问题拆分为小问题,对于小问题要不断地试错。 这里,我们使用的foo[100][100]是指针,所以需要和vector对应,即prev[100][100],这样对应就不会出现上一个程序中的问题了,并且也不会存在重复创建vector的问题,即使到了100这个量级上,也不会使程序崩溃。这个程序运行之后,结果如下:

#include <iostream>
#include <windows.h>
#include <vector>
using namespace std;
struct Phi {
    int m; 
    double n;
};
int main() {
    vector<struct Phi>::iterator foo[100][100];
    vector<struct Phi> pvec[100][100];
    Phi phi;
    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            // 创建结构体
            phi.m = i; 
            phi.n = 0.1;
            pvec[i][j].push_back(phi); 
            
            phi.m = i;
            phi.n = 0.2;
            pvec[i][j].push_back(phi);
            
            phi.m = i;
            phi.n = 0.7;
            pvec[i][j].push_back(phi);
            
            foo[i][j] = pvec[i][j].begin(); 
        }
    }
    cout << (foo[88][88] + 1)->m << endl;
    cout << (foo[88][88] + 1)->n << endl;
    system("pause");
}

  

  另外,我们还可以做下面简单的操作:

 

 

 

 

#include <iostream>
#include <windows.h>
#include <vector>
using namespace std;
struct Phi {
    int m; 
    double n;
};
int main() {
    vector<struct Phi>::iterator foo[200][200];
    vector<struct Phi> pvec[200][200];
    Phi phi;
    for (int i = 0; i < 200; i++) {
        for (int j = 0; j < 200; j++) {
            // 创建结构体
            phi.m = i; 
            phi.n = 0.1;
            pvec[i][j].push_back(phi); 
            
            phi.m = i + 1;
            phi.n = 0.2;
            pvec[i][j].push_back(phi);
            
            phi.m = i + 2;
            phi.n = 0.7;
            pvec[i][j].push_back(phi);
            
            foo[i][j] = pvec[i][j].begin(); 
        }
    }
    // 通过指针访问
    cout << (foo[185][88] + 1)->m << endl;
    cout << (foo[188][88] + 1)->n << endl;
    cout << endl;

    // 通过at()访问
    cout << pvec[185][88].at(1).m << endl;
    cout << pvec[185][88].at(1).n << endl;
    cout << endl;

    // 访问(185, 88)的所有结构体元素
    for (foo[185][88] = pvec[185][88].begin(); foo[185][88] != pvec[185][88].end(); foo[185][88]++) {
        cout << foo[185][88]->m << " " << foo[185][88]->n << endl;
    }
    cout << endl;

    // 添加一个结构体
    phi = {88, 0.0};
    pvec[185][88].push_back(phi);

    phi = {52, 10e-8};
    pvec[185][88].insert(pvec[185][88].begin() + 1, phi);

    // 打印结果
    for (foo[185][88] = pvec[185][88].begin(); foo[185][88] != pvec[185][88].end(); foo[185][88]++)
    {
        cout << foo[185][88]->m << " " << foo[185][88]->n << endl;
    }
    cout << endl;

    // 删除值小于10e-5的结构体
    for (foo[185][88] = pvec[185][88].begin(); foo[185][88] != pvec[185][88].end(); foo[185][88]++)
    {
        if (foo[185][88]->n < 10e-5) {
            pvec[185][88].erase(foo[185][88]);
            foo[185][88]--;
        }
    }
    cout << endl;

    // 打印结果
    for (foo[185][88] = pvec[185][88].begin(); foo[185][88] != pvec[185][88].end(); foo[185][88]++)
    {
        cout << foo[185][88]->m << " " << foo[185][88]->n << endl;
    }
    cout << endl;

    system("pause");
}

  注意,这里用了phi = {}的形式给结构体赋值,就要求使用下面方式调用,即使用c++11

PS C:\Users\Administrator\Desktop> g++ t.cpp -o t -std=c++11
PS C:\Users\Administrator\Desktop> start t

  结果如下:

186
0.2

186
0.2

185 0.1
186 0.2
187 0.7

185 0.1
52 1e-007
186 0.2
187 0.7
88 0


185 0.1
186 0.2
187 0.7

  即我们可以在这个结构体数组中插入、删除、访问等。

 

  另外,如果是结构体vector,我们也是可以使用sort函数进行排序的,需要引入头文件 <algorithm>,如下:

#include <iostream>
#include <windows.h>
#include <vector>
#include <algorithm>
using namespace std;
struct Phi {
    int m; 
    double n;
};
bool LessSort(Phi a, Phi b) {
    return (a.n > b.n);
}
int main() {
    vector<struct Phi>::iterator foo[200][200];
    vector<struct Phi> pvec[200][200];
    Phi phi;
    for (int i = 0; i < 200; i++) {
        for (int j = 0; j < 200; j++) {
            // 创建结构体
            phi.m = i + 2; 
            phi.n = 0.1;
            pvec[i][j].push_back(phi); 
            
            phi.m = i;
            phi.n = 0.2;
            pvec[i][j].push_back(phi);
            
            phi.m = i + 1;
            phi.n = 0.7;
            pvec[i][j].push_back(phi);
            foo[i][j] = pvec[i][j].begin(); 
        }
    }
    // 通过指针访问
    cout << (foo[185][88] + 1)->m << endl;
    cout << (foo[188][88] + 1)->n << endl;
    cout << endl;

    // 通过at()访问
    cout << pvec[185][88].at(1).m << endl;
    cout << pvec[185][88].at(1).n << endl;
    cout << endl;

    // 访问(185, 88)的所有结构体元素
    for (foo[185][88] = pvec[185][88].begin(); foo[185][88] != pvec[185][88].end(); foo[185][88]++) {
        cout << foo[185][88]->m << " " << foo[185][88]->n << endl;
    }
    cout << endl;

    // 添加一个结构体
    phi = {88, 0.0};
    pvec[185][88].push_back(phi);

    phi = {52, 10e-8};
    pvec[185][88].insert(pvec[185][88].begin() + 1, phi);

    // 结构体vector排序
    sort(pvec[185][88].begin(), pvec[185][88].end(), LessSort);

    // 打印结果
    for (foo[185][88] = pvec[185][88].begin(); foo[185][88] != pvec[185][88].end(); foo[185][88]++)
    {
        cout << foo[185][88]->m << " " << foo[185][88]->n << endl;
    }
    cout << endl;

    // 删除值小于10e-5的结构体
    for (foo[185][88] = pvec[185][88].begin(); foo[185][88] != pvec[185][88].end(); foo[185][88]++)
    {
        if (foo[185][88]->n < 10e-5) {
            pvec[185][88].erase(foo[185][88]);
            foo[185][88]--;
        }
    }
    cout << endl;

    // 打印结果
    for (foo[185][88] = pvec[185][88].begin(); foo[185][88] != pvec[185][88].end(); foo[185][88]++)
    {
        cout << foo[185][88]->m << " " << foo[185][88]->n << endl;
    }
    cout << endl;

    system("pause");
}

  如上是一个降序排序,当然也可以进行升序排序。

   除此之外,如果我们希望调用结构体vector的数组,如下所示:

#include <iostream>
#include <windows.h>
#include <vector>
#include <algorithm>
using namespace std;
// 结构体声明
struct Grain {
    int key; 
    double phi;
};

const int N = 7;
const int Nx = 300;
const int Ny = 300;

// 排序函数
bool LessSort(Grain a, Grain b) {
    return (a.phi > b.phi);
};

// 调用grid[][]

void foo(vector<struct Grain> grid[][Ny], vector<struct Grain>::iterator it[][Ny], int N, int Nx, int Ny);

int main() {
    // 结构体vector
    vector<struct Grain> grid[Nx][Ny];
    // 结构体vector迭代器
    vector<struct Grain>::iterator it[Nx][Ny];

    Grain grain;
    
    // 每个格点都是一个vector,赋值
    for (int i = 0; i < Nx; i++) {
        for (int j = 0; j < Ny; j++) {
            // 创建结构体
            grain.key = i + 2; 
            grain.phi = 0.1;
            grid[i][j].push_back(grain); 
            
            grain.key = i;
            grain.phi = 0.2;
            grid[i][j].push_back(grain);
            
            grain.key = i + 1;
            grain.phi = 0.7;
            grid[i][j].push_back(grain);
            it[i][j] = grid[i][j].begin(); 
        }
    }

    // 推荐! 通过指针访问,即使超出边界也不会崩溃,值为0。
    cout << (it[185][88] + 1)->key << endl;
    cout << (it[188][88] + 3)->phi << endl;
    cout << endl;

    // 不推荐!通过at()访问,如果超出边界则会程序崩溃,另外,使用下标也不会崩溃,但是用指针更好
    cout << grid[185][88].at(0).key << endl;
    cout << grid[185][88].at(0).phi << endl;
    cout << endl;

    // 访问(185, 88)的所有结构体元素
    for ( it[185][88] = grid[185][88].begin(); it[185][88] != grid[185][88].end(); it[185][88]++) {
        cout << it[185][88]->key << " " << it[185][88]->phi << endl;
    }
    cout << endl;

    // 添加一个结构体
    grain = {88, 0.0};
    grid[185][88].push_back(grain);

    grain = {52, 1e-7};
    grid[185][88].insert(grid[185][88].begin() + 1, grain);

    // 结构体vector排序
    sort(grid[185][88].begin(), grid[185][88].end(), LessSort);

    // 打印结果
    for (it[185][88] = grid[185][88].begin(); it[185][88] != grid[185][88].end(); it[185][88]++)
    {
        cout << it[185][88]->key << " " << it[185][88]->phi << endl;
    }
    cout << endl;

    // 删除值小于10e-5的结构体
    for (it[185][88] = grid[185][88].begin(); it[185][88] != grid[185][88].end(); it[185][88]++)
    {
        if (it[185][88]->phi < 10e-5) {
            grid[185][88].erase(it[185][88]);
            it[185][88]--;
        }
    }
    cout << endl;

    // 打印结果
    for (it[185][88] = grid[185][88].begin(); it[185][88] != grid[185][88].end(); it[185][88]++)
    {
        cout << it[185][88]->key << " " << it[185][88]->phi << endl;
    }
    cout << endl;

    foo(grid, it, N, Nx, Ny);

    system("pause");
}

void foo(vector<struct Grain> grid[][Ny], vector<struct Grain>::iterator it[][Ny], int N, int Nx, int Ny) {
    cout << grid[185][88].at(0).key << endl;
    cout << grid[185][88].at(0).phi << endl;
    // 使用迭代器访问
    it[185][88] = grid[185][88].begin();
    cout << it[185][88]->key << endl;
    cout << it[185][88]->phi << endl;
    cout << "66666666666666" << endl;
}

  即定义函数的时候,结构体数组参数只需要直接传入,不需要使用 &,这个方式和一般的多维数组的定义方式是一致的。

  

  另外,我们在用for循环的时候,格式并不是通常那么简单,也可以是下面这样的:

    for (it[185][88] = grid[185][88].begin(), it_b[185][88] = grid_b[185][88].begin(); it[185][88] != grid[185][88].end(); it[185][88]++, it_b[185][88]++)
    {
        cout << it[185][88]->key << " " << it[185][88]->phi << "grid" << endl;
        cout << it_b[185][88]->key << " " << it_b[185][88]->phi << "grid_b" << endl;
    }
    cout << endl;

  如上所示,我们以it[188][85]为限定变量,但是完全也可以让 it_b[185][88] 赋初值并且让其累加,只是不参与其中的计算而已。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

posted @ 2018-05-11 17:19  Wayne-Zhu  阅读(1553)  评论(0编辑  收藏  举报

一分耕耘,一分收获。