my example code is:
#include <bits/stdc++.h>
using namespace std;
void print_vec(vector<vector<int>> v) {
cout << "**********print start!!!**********" << endl;
for (int i = 0; i < v.size(); ++i) {
for (int j = 0; j < v[i].size(); ++j) {
cout << v[i][j] << " ";
}
cout << endl;
}
cout << "**********print end!!!**********" << endl;
}
void change_vec(vector<vector<int>>& v) {
for (int i = 0; i < v.size(); ++i) {
for (int j = 0; j < v[i].size(); ++j) {
v[i][j] *= 10000;
}
}
}
void change_vec(vector<vector<int>>& v, int pos1, int pos2) {
for (int i = pos1; i < pos2; ++i) {
for (int j = 0; j < v[i].size(); ++j) {
v[i][j] *= 10000;
}
}
}
int main() {
vector<vector<int>> block_values = {{99,98},{97,96},{95,94},{93,92},{91,90},{89,88},{87,86},{85,84},{83,82},{81,80}};
vector<std::vector<int>> sub = vector<std::vector<int>>(block_values.begin() + 1, block_values.begin()+3);
print_vec(sub);
change_vec(sub);
change_vec(block_values, 6, 7);
print_vec(sub);
print_vec(block_values);
}
The output is:
**********print start!!!**********
97 96
95 94
**********print end!!!**********
**********print start!!!**********
970000 960000
950000 940000
**********print end!!!**********
**********print start!!!**********
99 98
97 96
95 94
93 92
91 90
89 88
870000 860000
85 84
83 82
81 80
**********print end!!!**********
The modification from sub
does not affect block_values
. So my question is how can I initialize sub
so that when I change values of sub
, block_values
also change? Thanks!
I compile this program and want: the change of the vector slice can "persist" on the whole vector.