| |

VerySource

 Forgot password?
 Register
Search
View: 1506|Reply: 11

How to use iterator to operate a two-dimensional array created by vector?

[Copy link]

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-15 15:40:01
| Show all posts |Read mode
Define a two-dimensional array like this:
vector <vector <int>> ivec;

How should it be initialized with iterator?
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-19 00:36:01
| Show all posts
for (vector <vector <int>> :: iterator iter = ivec.begin (); iter! = ivec.end (); ++ iter)
{
 for (vector <int> :: iterator iiter = (* iter) .begin (); iiter! = (* iter) .end (); ++ iiter)
 {
  * iiter = ??;
  ..........
 }
}
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-1-19 23:36:01
| Show all posts
vector <int> ivec (10);
One-dimensional array can set the storage space of the array like this, what about two-dimensional?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-22 13:27:01
| Show all posts
vector <vector <int>> ivec (2);
typedef vector <int> :: iterator Iter;
typedef vector <vector <int>> :: iterator IIter;
IIter iiter;
Iter iter;
for (iiter = ivec.begin (); iiter! = ivec.end (); iiter ++)
{
(* iiter) .resize (3);
for (iter = (* iiter) .begin (); iter! = (* iiter) .end (); iter ++)
cin >> (* iter);
}
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-22 14:09:01
| Show all posts
vector <vector <int>> ivec;
The iterator of the first dimension points to vector <int>, and the iterator of the second dimension points to int.
Reply

Use magic Report

0

Threads

24

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-25 16:00:02
| Show all posts
vector <vector <int>> ivec;
-------------------------------------------------- -----------------------------------
Here you define an empty vector, which cannot be initialized with iterator, you should use push_back operation to add elements.
Alternatively, instead of defining an empty vector, you can give an initial size.
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-1-27 16:36:01
| Show all posts
Also askboxer2005:
For the001250200method vector <vector <int>> ivec (2) ;, this is to define a two-dimensional two-dimensional array, but does not specify the number of columns. How should we define a two-dimensional array with specified rows and columns What about arrays?
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-27 18:18:01
| Show all posts
The vector specifies that the rows and columns are not significant, it can always change at any time.
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-27 19:54:02
| Show all posts
If the dimensional information of your data can be determined at compile time, and it will not change thereafter, then using a native array is also good.
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 China

Post time: 2020-1-27 23:00:01
| Show all posts
Specifying multidimensional at the beginning can be like this:

vector <vector <int>> a (3, vector <int> (4)); // Equivalent to a 3 × 4 integer array.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list