Click here to Skip to main content
15,886,036 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
what is the equivalent function in vector for SetAtGrow in CArray i have to use vector insted of CArray

Add
SetAtGrow
InsertAt
Posted
Updated 14-Jul-15 19:00pm
v3
Comments
Sergey Alexandrovich Kryukov 15-Jul-15 0:42am    
What have you tried so far?
—SA
[no name] 15-Jul-15 0:58am    
i tried resize , size etc .... i want equivalent apis
Sergey Alexandrovich Kryukov 15-Jul-15 1:00am    
If you tried anything, we would see your code sample with explanation of some particular problem.
—SA

1 solution

Simply read the documentation on CArray::SetAtGrow and do exactly what it does for a vector.

  1. Get the current size of the vector: http://www.cplusplus.com/reference/vector/vector/size/[^].
  2. Compare the size with the required index. If index is more than size − 1, find out how many elements you need to add to place the element at the index.
  3. If the number of elements to add is greater then zero, add appropriate number of missing elements: http://www.cplusplus.com/reference/vector/vector/push_back[^].

    It make require you to add more elements than one. One element (at the index of newSize − 1) is your function argument. What are the additional elements, is up to you. The documentation of SetAtGrow does not clearly specify it. You may or may not experiment with it, but it's way more important to do what you really want.
  4. If you don't need to add more elements, you have to replace some existing element at index with new element. Do it: http://www.cplusplus.com/reference/vector/vector/assign[^].


Please see: http://www.cplusplus.com/reference/vector/vector[^].

Now, one little note: first of all, think about reviewing your code. Chances are, the code using CArray::SetAtGrow is not well designed. The functionality of this function is questionable and looks like too much of ad-hoc developers of MFC used for their internal purposes. I would never use it. Just some food for thought.

Good luck.
—SA
 
Share this answer
 
v4
Comments
Stefan_Lang 15-Jul-15 2:34am    
I totally agree with your note: I see no usecase for this function at all - any use case I can think of would either know the array size in advance or use a different type of container.
Sergey Alexandrovich Kryukov 15-Jul-15 15:45pm    
That different type of collection could be the one based on key-value pairs (associative array, dictionary). Then the value elements can be indexed with some arbitrary index objects (with search based on hash values and buckets, which gives slower access, but still O(1) time complexity), and these elements does not have to the sequence without gaps. Say, if "indices" (keys) are 1, 2, 3, 100, there are only 4 elements.

But the inquirer's idea to get rid of MFC collections and use std::* is good. Unfortunately, MFC UI requires MFC type, but who reasonably and seriously needs MFC UI? only the owners of big legacy?

—SA
Stefan_Lang 16-Jul-15 2:07am    
I agree. I've been avoiding MFC for many years to the best of my ability. Nothing in that outdated API is up to modern standards!
Sergey Alexandrovich Kryukov 16-Jul-15 2:16am    
I understand, I did exactly the same. By the way, for native-code UI I recently came back to Object Pascal, a major .NET prototype (Turbo Pascal and then Delphi chief architect Anders Hejlsberg also was the chief architect of C#). I found Free Pascal and Lazarus in excellent condition (for example, truly multi-platform, with excellent UI abstraction layer), far more advanced than Delphi of the time when I mostly switched to .NET.
—SA
[no name] 19-Jul-15 17:29pm    
this idiot Sergey Alexandrovich Kryukov is posting whatever he wants, complete bullsh*t. Get away from here dirty ship!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900