![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#1
|
|||
|
|||
How to display and expand the elements in a 2D matrix presented in C++ vector form.Hi,
I tried to present a 2D mathematic matrix with C++ vector and expand it by repeating the first and last row and column. An example for intuition, I initialized a 2D C++ vector vec_2d to be as following: 1, 2, 3, 4 5, 6, 7, 8 9,10,11,12 13,14,15,16, and I want to expand it to be: 1, 1, 2, 3, 4, 4 1, 1, 2, 3, 4, 4 5, 5, 6, 7, 8, 8 9, 9,10,11,12,12 13,13,14,15,16,16 13,13,14,15,16,16 by first repeat the first and last rows and then the updated first and last columns. The code is as following: CPP / C++ / C Code:
I have written the code to expand the vector by repeating the first and last rows but I do not know how to repeat the first and last columns. Another problem is, I do not know how to check if I am right by printing all the elements in the vector, the code I have written for this purpose seems to work badly. Anyone can tell me why and help me out? Thank you in advance. nanchuangyeyu |
|||
|
#2
|
|||
|
|||
Re: How to display and expand the elements in a 2D matirx presented in C++ vector form.Quote:
Change this CPP / C++ / C Code:
CPP / C++ / C Code:
You really meant to use "<" in the loop control, as you did in the outer loop, didn't you? Anyhow, the middle part of the for() must be an expression that has a boolean value or an expression whose value can be converted to a boolean. Your expression it_ld=(*it).end() has a value that is an iterator. You can't convert a iterator to a boolean even if you wanted to for some unearthly reason, and the compiler should give an error here. The fact that VisualC++ version 6 does not even give so much as a warning is another reason that I really don't like to recommend that compiler as a learning tool. Later versions of the Microsoft compilers correctly flag this as an error, as do all other "decent and modern" C++ compilers (Borland, GNU, etc.). With this correction, your output should look something like Code:
Quote:
You can use index notation. Now since vec_2d is a vector of vectors, each element of vec_2d is a vector. That is: vec_2d[0] is a vector vec_2d[1] is a vector etc. In matrix terminology, vec_2d[i] is denoted the ith row of the matrix. So you can print the matrix a row at a time with something like CPP / C++ / C Code:
The output, using your matrix would be something like Code:
Now one final note: With a vector of vectors, it may be possible that not all of the 'rows' have the same number of elements. With matrices, they do. Let's say that a particular matrix has numrows rows and numcols columns. Then, after noting the hints in the comments my little example, the print loop could be written a little more elegantly as something like CPP / C++ / C Code:
The index notation does not test index values to see whether they are out of range, so the programmer must be certain that the sizes are correct, otherwise undefined behavior can result. Keeping loop limits as in the previous example (using .size() as the loop conditions) will avoid going out of range, so some people prefer the slightly less elegant method. Chacun à son goût! Regards, Dave Last edited by davekw7x : 05-May-2009 at 10:05.
|
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Initializing All Array Elements to Zero vs. Other Numbers? | BobbyMurcerFan | C Programming Language | 6 | 17-Dec-2004 21:00 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The