Thread: Access specific element in for each loop

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Access specific element in for each loop

    Hello

    Can you access only a specific element in a for each loop?

    For example how would I print only the second element in the Names array?

    Code:
    #include <windows.h>
    #include <array>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        std::array<std::string, 3> Names = {"Harry ", "Dick", "Tom"};
    
        for (auto& name : Names)
        {
              cout << name << '\n';
        }
    
         return 0;
    
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like
    cout << Names[1] << endl;

    If you really need to be subscript aware, then use a subscript
    for ( size_t i = 0 ; i < Names.size() ; i++ )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    No, Names[1] will access the second letter in each name, returns:
    a
    i
    o

    I didnt express myself clear enough, I meant second element of the list.

    I would like to access "Dick".

    I guess we need to use a subscript for this then.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    *shrug*
    Code:
    #include <array>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        std::array<std::string, 3> Names = {"Harry ", "Dick", "Tom"};
    
        for (auto& name : Names)
        {
              cout << name << '\n';
        }
    
        cout << "Names[1]=" << Names[1] << endl;
    
        return 0;
    }
    
    $ g++ foo.cpp
    $ ./a.out 
    Harry 
    Dick
    Tom
    Names[1]=Dick
    Don't confuse Names[1] with name[1]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Ah, of course, thanks!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help pointer& structure(access of element)
    By mohsen in forum C Programming
    Replies: 3
    Last Post: 07-30-2011, 03:49 PM
  2. how to print/access the 1st element of the first set of the vector
    By kapil1089thekin in forum C++ Programming
    Replies: 1
    Last Post: 08-11-2010, 02:28 PM
  3. Using pointer to access list element
    By GOBLIN-85 in forum C++ Programming
    Replies: 3
    Last Post: 03-03-2008, 04:15 AM
  4. Matrix element access
    By nepper271 in forum C++ Programming
    Replies: 14
    Last Post: 02-01-2008, 09:08 AM
  5. Xerces XML: Need to access element value
    By pipercubusa in forum C++ Programming
    Replies: 0
    Last Post: 04-01-2005, 12:39 PM

Tags for this Thread