|
I am in the process of learning how to use arrays. To be more specifc, how to modify the code of "for loops" to learn different ways to work with arrays. Listed below is a basic program where I am attempting to learn how to modify a two dimensional array. I would like to place a 1 in the first dimension and a 2 in the second dimension.
The issue I do not understand is why my for loop is advancing to array element 2. Shouldn't the elements updated through the loop be [0] and [1]? Shouldn't my for loop stop when it encounters the for statement test condition <=1?
What is happening is that the statement advances to element [2]. Do I have a correct understanding of the for loop? Advice is much appreciated.
#include <iostream>
using std::endl;
using std::cout;
using std::cin;
int initialize[2][2];
int row;
int col;
int main()
{
for(row=0;row<=1;row++)
{
for(col=0;col<=1;col++)
{
initialize[row][col] = 1;
cout << "Row ""Col "<< row << col << initialize[row][col] << '\n';
}
}
}
|
|
|
|
|
A loop such as for(row=0;row<=1;row++) should be read like this: 'Set row to 0, and then keep adding 1 to row until row is no longer less or equal to 1.' So what happens is this:
First iteration (row is now 0):
Is row <= 1? Yes; execute the code in the loop's body and when that's done, add 1 to row.
Second iteration (row is now 1):
Is row <= 1? Yes; execute the code in the loop's body and when that's done, add 1 to row.
Third iteration (row is now 2):
Is row <= 1? No; execute the code after the loop.
So although row is equal to 2 áfter the loop, row is always less or equal to 1 within the loop.
modified 13-Sep-18 21:01pm.
|
|
|
|
|
I am a bit unclear on the issue of Arrays vs vectors. Recently, I have started learning STL. I am more familiar with MFC. What is the big difference between vectors and the CArray, Cstring and std::string.
Vectors vs CArrays: CArray has all the tools that I need just like vectors and to me it looks more straight forward than vectors, but thats only me. I do realize there is a problem with copy constructors in CArrays. I know CArray is derived from objects and I am guessing vecto is not. So, does this make vectors a lot more efficient interms of memory usage?
CString vs std::string: same goes for strings, even though I find that I can do more with CString.
Besides portability, I don't see a major advantage of vectors.
Can someone clarify the real differences for me?
Thanks
modified on Friday, December 17, 2010 1:04 PM
|
|
|
|
|
All things that you noted are correct.
One of the design consideration of STL was to separate algorithms from containers.
So there are just one copy of algorithms that work with a whole range of containers.
They use iterators for this to happen.
So this makes it very easy to change containers if necessary.
|
|
|
|
|
It's a question of modernity; MFC collections are obsolete.
STL is the standard template library.
STL is faster (test in release, not debug mode) and offer more flexibility (algorithms, iterators, ...)
here's a link to a discussion :
http://www.codeguru.com/forum/archive/index.php/t-391319.html[^]
"...And frankly the team will give you the same answer. The MFC collection classes are only there for backwards compatibility. C++ has a standard for collection classes and that is the Standards C++ Library. There is no technical drawback for using any of the standard library in an MFC application.
We do not plan on making significant changes in this area.
Ronald Laeremans
Acting Product Unit Manager
Vsual C++ Team..."
Watched code never compiles.
|
|
|
|
|
As once said distinguished Mr. Grauss: "MFC containers are a crap". I do believe he was right.
STL container design is more rigorous and clean.
Now you feel more comfortable with CArrays , etc.., but, once get used with STL , you won't go back.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hi,
I am using DT_CALCRECT in DrawText() function. If I use
dc.DrawText(m_szText,rect,DT_LEFT|DT_WORDBREAK); It works fine.
But If I use dc.DrawText(m_szText,rect,DT_LEFT|DT_WORDBREAK|DT_CALCRECT);
I am having issue, text is not getting displyed.
What is the problem?
|
|
|
|
|
Read the documentation.
Using DT_CALCRECT will only compute the RECT needed for the text pass to DrawText, not draw the text.
usual workflow is to call CDC::DrawText with DT_CALRECT to get the valid RECT and then do something with it, normally call CDC::DrawText with the good RECT.
Watched code never compiles.
|
|
|
|
|
|
Please rephrase your questions.
|
|
|
|
|
Why do you repost your questions all the time and don't even bother to look at the replies ?
|
|
|
|
|
|
I would add some log statements (print to output window, or print to file). If you do this for more than one project, make sure to include timing information, I typically have the current time in "HH:mm:ss.fff" format.
Yes, it takes some effort adding the lines, and they do influence timing a bit, however they are a big help.
BTW: I don't remove them, I just disable them when I (think I) no longer need them.
|
|
|
|
|
|
I don't care about the expected answer, I'm only offering a solution. You can turn it down and continue waiting for someone to tell you whatever it is you do expect.
|
|
|
|
|
That's is rude.
We're trying to help; if you do not want help, don't ask questions.
Watched code never compiles.
|
|
|
|
|
Nami.v.s wrote: Sorry i think this is not the expected answer.
Maybe you should explain what is the expected answer, so someone can find it for you.
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
knowing the answer to a question is half-cheating!
Watched code never compiles.
|
|
|
|
|
Huh ?
I'm not certain I understand the question, or what you are actually doing...
Are you saying you have the same code compiled on VC6 and VS2008 that generate different results ?
If you are generating a DLL, you need to have an executable that will call the code in the DLL and check that.
One way to do that, is to use log file.
Another way is to use unit tests.
Good luck.
Watched code never compiles.
|
|
|
|
|
At What Conditions,
CStudioFile::ReadString() Gives/Returns the FALSE value?
And
In a File Consists the Lines In one Condition "m_pString" doesn't take the line in the File?
Why it Happen? Any body know Please give me Reply....
|
|
|
|
|
John502 wrote: At What Conditions,
CStudioFile::ReadString() Gives/Returns the FALSE value
According to the documentation:
Return Value
A pointer to the buffer containing the text data. NULL if end-of-file was reached without reading any data; or if boolean, FALSE if end-of-file was reached without reading any data.
John502 wrote: In a File Consists the Lines In one Condition "m_pString" doesn't take the line in the File?
Not sure what that means. Could you re-phrase that question?
Go and never darken my towels again - Groucho Marx
|
|
|
|
|
hello sir,
I know that,(i.e)already in the MSDN ,
AnyBody Know the another Conditions Please,
Tell me.
|
|
|
|
|
how to compare two frames of any media file in vc++
|
|
|
|
|
what do you mean by "compare" ?
|
|
|
|
|
it depends on how you represent the decoded frames. It can be by BITMAPINFOHEADER like structures..
|
|
|
|