Thread: The code from the internet, it's nonsense, or is it?

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    119

    The code from the internet, it's nonsense, or is it?

    Code:
    #include <algorithm>
    #include <iostream>
    #include <list>
    #include <string>
    
    int main()
    {
        int array[ 17 ] = { 7,3,3,7,6,5,8,7,2,1,3,8,7,3,8,4,3 };
    
        int elem = array[ 9 ];
        int *found_it;
    
        found_it = std::find( &array[0], &array[17], elem );
    
        // печатается: поиск первого вхождения 1 найдено!
    
        std::cout <<  "поиск первого вхождения "
             << elem <<"\t"
             <<  ( found_it ? "найдено!\n" : "не найдено!\n" );
    
         std::string beethoven[] = {
            "Sonata31", "Sonata32", "Quartet14", "Quartet15",
           "Archduke", "Symphony7" };
    
        std::string s_elem( beethoven[ 1 ] );
    
         std::list< std::string, std::allocator > slist( beethoven, beethoven+6 );
        std::list< std::string, std::allocator >::iterator iter;
    
        iter = find( slist.begin(), slist.end(), s_elem );
    
        // печатается: поиск первого вхождения Sonata32 найдено!
    
        std::cout <<  "поиск первого вхождения "
             <<  s_elem <<  "\t"
             <<  ( found_it ? "найдено!\n" : "не найдено!\n" );
    }

    The code from the internet, it's nonsense, or is it?

    std::list< std::string, std::allocator > slist( beethoven, beethoven+6 );&&????????

  2. #2
    Guest
    Guest
    Have you tried compiling it? The code looks to be using a particular constructor that takes two iterators, in this case the range from the first element, to the 6th of the beethoven array.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    It should be
    Code:
    std::list< std::string, std::allocator<std::string> >
    although the std::allocator<std::string> part is not necessary since that's what it would default to.

    Maybe the <std::string> part was hidden because it was interpreted as a tag by the webpage. Do you have the link?

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Which part of that line of code looks like nonsense?

    In the STL, containers like `std::list` are called "allocator-aware" which means that you can swap out the class that the container uses for its allocation routines.

    You can also create most containers from a pair of iterators and all pointers are iterators (they're technically RandomAccessIterators which are all InputIterators from the STL's point of view). Keep in mind that in C/C++, array objects will implicitly decay into a pointer type.

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    119
    Thank you, thank you very much!

    I'm still just learning. So a lot of code seems strange or unusual, incomprehensible to me.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    This is why using using namespace std is a boon. Don't listen to the Internet when they tell you not to use it.

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Well, the general advice around using namespaces explicitly is because C++ has overloading so one function or operator may have multiple definitions scattered through the codebase, including separate namespaces. So there's a danger in refactoring that you'll wind up calling something you didn't mean to. As long as the compiler can find a suitable definition, it'll call whatever it's found.

    The STL is also full of somewhat generic and common names so the danger increases even more. You just gotta be careful, is all. Make sure you also never have a `using` statement in something like a header file as well.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Quote Originally Posted by c99tutorial View Post
    This is why using using namespace std is a boon. Don't listen to the Internet when they tell you not to use it.
    Student assignments that only use std can usually get away with being lazy.

    Large projects with many namespaces can find mass import of std a complete hassle.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-03-2014, 08:13 PM
  2. Programmer Wanted - Someone Who Is Tired Of Nonsense
    By jwgamedesign in forum Projects and Job Recruitment
    Replies: 7
    Last Post: 06-06-2013, 05:35 AM
  3. Conspiracy? Or just more nonsense?
    By minesweeper in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 12-15-2002, 08:56 PM

Tags for this Thread