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

Thread: vector at

  1. #1
    Join Date
    Aug 1999
    Location
    Romania, Bucharest
    Posts
    253

    vector at

    how to use vector.at
    i try this and doesn't work
    mt arr has 100000 objects

    void CTestDlg::OnButton1()
    {
    CString s;
    s.Format("\n%d", arr.at(49999).Xdeg);
    AfxMessageBox(s);
    }



  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: vector at


    #include <vector>
    #include <iostream>
    #include <exception>

    #include <conio.h>

    int main()
    {
    std::vector<int> vecInteger;

    // Fill vector
    for(int iIndex = 0; iIndex < 50; ++iIndex)
    vecInteger.push_back(iIndex);

    // Get one element
    try
    {
    std::cout << vecInteger.at(20) << std::endl;

    }
    catch(const std:ut_of_range &)
    {
    std::cout << "Out of range access" << std::endl;
    }

    // Wait for keystroke
    _getch();

    return 0;
    }




    Ciao, Andreas

    "Software is like sex, it's better when it's free." - Linus Torvalds

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: vector at

    What do you mean "it doesn't work?". Please be more specific. Accessing object 49,999 from a vector of 100,000 is *not* the problem -- it is somewhere else. I can't compile your code because of the following:

    a) what is arr a vector of? int's, double's, widgets?

    b) what is Xdeg? Please show the structure that you are referring to.

    b) How do you initialize this vector? Are you sure it contains 100000 objects?

    Basically, show the miminal amount of code that lets everyone see what you're talking about.

    Regards,

    Paul McKenzie


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