CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Array of array

  1. #1
    Join Date
    Jan 2009
    Posts
    399

    Array of array

    Hi all of you.
    I have to use an array of array, the second one has one type of array (array of double), and first one array of int:
    Code:
    CArray<double, double> arrSum;
    
    CArray <?, ?> arr;
    
    arrSum.Add(4.3);
    arrSum.Add(5.7);
    arrSum.Add(1.9);
    arrSum.Add(0.3);
    
    arr.Add(arrSum);
    
    arrSum.Add(4.5);
    arrSum.Add(5.5);
    arrSum.Add(8.3);
    arrSum.Add(1.5);
    
    arr.Add(arrSum);
    and I need few arr elements, every one of them need to have CArray<double, double> elements.

    I have done something like this:
    Code:
    	CArray<double, double>* pArrSum;
    	CTypedPtrArray<CPtrArray, CArray<double, double>*> arr;
    
    pArrSum = new CArray<double, double>;
    pArrSum->Add(4.3);
    pArrSum->Add(5.7);
    pArrSum->Add(1.9);
    pArrSum->Add(0.3);
    
    arr.Add(pArrSum);
    ...
    and seem to go ... my question is, is there another way to solve this issue, in an simpler way ? Preferable, in MFC way (I use this in an MFC app), not STL ...

    Thank you.
    Last edited by mesajflaviu; October 15th, 2017 at 11:51 AM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Array of array

    There's really no reason you can't use STL within an MFC app. The STL collections are way more evolved than the MFC ones (which originated when MFC was still 16 bit).

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Array of array

    As Arjay already suggested, I would like to advise: even in an MFC aplication, prefer STL containers over MFC collections whenever is possible. Since I said "do not use STL in MFC-based applications" has been a long time and STL evolved. Especially if your compiler is at least C++11 compliant, the resulting code is more optimal and easier to read and maintain. Just giving an example, this code...
    Code:
        const int nSize = m_arrMFC.GetSize();
        for (int nIndex = 0; nIndex < nSize; nIndex++)
        {
            const CFoo& foo = m_arrMFC.ElementAt(nIndex);
            foo.DoSomething();
            // ...
        }
    ...does the same as
    Code:
        for (const auto& foo : m_arrSTL)
        {
            foo.DoSomething();
            // ...
        }
    It's just a trivial example but it already contains two less lines of code.

    So, declare std::vector<std::vector<double>> and go ahead!
    If you are not yet familiarized and/or feel comfortable with STL stuff, it's time...
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    Jan 2009
    Posts
    399

    Re: Array of array

    Thank you all for your answers.
    "declare std::vector<std::vector<double>>" Good idea !
    "If you are not yet familiarized and/or feel comfortable with STL stuff, it's time" ... you are right ... I'll try to change my compiler first, I am working on VS2008 right now ... however, std::vector<std::vector<double>> it's ok with VS2008.
    Last edited by mesajflaviu; October 24th, 2017 at 10:42 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured