Thread: What is the C++ equivalent for Python...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    67

    Smile What is the C++ equivalent for Python...

    Hi everyone

    In Python, I know how the following works:

    Code:
    >>> SET = []
    >>> SET.append(1)
    >>> print SET
    [1]
    An empty set is first defined.
    The integer 1 is added/appended to the set.
    The set is printed, having one new element instead of being empty.

    How would I go about doing this in C++?
    Would it look something like this?

    Code:
    #include <iostream>
    #include <set>
    using namespace std;
    
    int main()
    {
        set<int> SET;
        SET.insert(1)
        cout << SET << endl;
    }

    I tried doing that but I got an error saying
    no match for 'operator<<' in 'std::cout << SET'
    I am using DevC++ 4.9.9.2 and Python 2.7

    Thank you all in advance

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'll have to walk the set and print each member.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    How can I append new elements to the set as shown in the Python example?
    Is there a way to declare an empty set and add new members to it?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Unfortunately, you cannot print a std::set using operator<<. You do have a few options, e.g., manually write a loop to do it, or use a generic algorithm.

    By the way, I believe that the Python code defines a list, not a set. This would be more akin to a sequential container in C++ like an array, or std::vector, or std::list.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Despite the fact that you're calling that variable SET, it is not a set. It's a list. The appropriate C++ container would be std::vector<> not std::set<>.

    (Python "lists" are actually arrays.)

    EDIT: Doof. Laserlight already pointed that out.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You could also overload an operator << for a set, or vector or whatever. Then you could call cout << my_set;
    There is no predefined, but make it once, use it always.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    Thanks guys.

    I'm still quite confused >_<

    I went ahead and looked up std::list at
    Cprogramming.com - STL Tutorial - List Container

    I copied and pasted the last portion:
    Code:
    std::list<int> int_list;
    int_list.push_back(1);
    int_list.push_back(1);
    int_list.push_back(8);
    int_list.push_back(9);
    int_list.push_back(7);
    int_list.push_back(8);
    int_list.push_back(2);
    int_list.push_back(3);
    int_list.push_back(3);
    int_list.sort();
    int_list.unique();
    
    for(std::list<int>::iterator list_iter = int_list.begin(); 
        list_iter != int_list.end(); list_iter++)
    {
        std::cout<<list_iter<<endl;
    }
    I have the same error as before (which is strange since I thought this would be a working model).
    I don't know all the terms being used in your answers.

    If it doesn't bother you guys, could you post the simplest sample code that would reflect the behaviour of defining a list/set/array and inserting elements to it?
    I understand that those datatypes slightly differ, but I'll look into those right now.
    I'm taking notes as I go, but things are a bit confusing still.

    Thanks for all your help. Much appreciated!

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by CPlus View Post
    Thanks guys.

    I'm still quite confused >_<

    I went ahead and looked up std::list at
    Cprogramming.com - STL Tutorial - List Container

    I copied and pasted the last portion:
    Code:
    std::list<int> int_list;
    int_list.push_back(1);
    int_list.push_back(1);
    int_list.push_back(8);
    int_list.push_back(9);
    int_list.push_back(7);
    int_list.push_back(8);
    int_list.push_back(2);
    int_list.push_back(3);
    int_list.push_back(3);
    int_list.sort();
    int_list.unique();
    
    for(std::list<int>::iterator list_iter = int_list.begin(); 
        list_iter != int_list.end(); list_iter++)
    {
        std::cout<<list_iter<<endl;
    }
    I have the same error as before (which is strange since I thought this would be a working model).
    I don't know all the terms being used in your answers.

    If it doesn't bother you guys, could you post the simplest sample code that would reflect the behaviour of defining a list/set/array and inserting elements to it?
    I understand that those datatypes slightly differ, but I'll look into those right now.
    I'm taking notes as I go, but things are a bit confusing still.

    Thanks for all your help. Much appreciated!
    You need this:
    Code:
    std::cout<<*list_iter<<endl;
    list_iter itself is not an object, it merely points to where the object is. You have to follow that pointer to find the object.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    You need this:
    Code:
    std::cout<<*list_iter<<endl;
    list_iter itself is not an object, it merely points to where the object is. You have to follow that pointer to find the object.
    * here is the dereference operator. When used in a declaration(eg, "char *ptr"), the asterisk indicates a pointer to a type.

    I think python uses references but not pointers, so the concept of pointers and dereferencing may be something new.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    Thanks! That worked.

    If int_list is the list, is there any way I can cout the Nth element in it?
    For example, if int_list was 1,1,2,6,24,120
    then
    int_list[0] would be 1,
    int_list[1] would be 1,
    int_list[2] would be 2,
    int_list[3] would be 6,
    int_list[4] would be 24,
    int_list[5] would be 120,

    Thanks in advance
    You guys are a lot of help.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    For that you should use std::vector which allows random access.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Most containers overload the index operator, so the syntax you listed should work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CPlus View Post
    If int_list is the list, is there any way I can cout the Nth element in it?
    Not with a list. With a vector, a deque, or a C array you can use [], eg:

    Code:
    vector<int> myray;
    [...]
    cout << myray[n];
    vector is actually your basic dynamic array most akin to whatever it is called in python (set or list; dunno why they didn't just call it an array). A deque may be more exactly so; a C++ STL list is actually less like an array than either the vector or the deque.
    Last edited by MK27; 03-31-2010 at 02:30 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't have random access for a list. But you can do the walking algorithm that you just did, but with a counter to tell you when to stop.

  15. #15
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    It works

    The code I used was:

    Code:
    int main()
    {
        vector<int> VECTOR;
        VECTOR.push_back(1);
        VECTOR.push_back(1);
        VECTOR.push_back(2);
        VECTOR.push_back(6);
        VECTOR.push_back(24);    
        cout << VECTOR[4] << endl;
    }
    Which outputted 24.


    I have another question that is related to all of this.
    In Python, a user can input a list, such as:

    Code:
    LIST = input("Input your list here: ")
    LIST.append(1)
    print LIST
    What this does is ask the user to input a list called LIST.
    Afterward, LIST has the number 1 appended to it.
    If the user writes [0,1,2,3,4,5] as their input LIST, the
    script outputs [0,1,2,3,4,5,1]

    Would the C++ equivalent look something like this?

    Code:
    int main()
    {
        // VECTOR defined here
        vector<int> VECTOR;
    
        // User inputs into VECTOR
        std::cin>>VECTOR;
    
        // Adds "1" into VECTOR
        VECTOR.push_back(1);
    
        // couts from 0 to Vector size
        for (i==0;i<(VECTOR.size());i++)
        {
            cout << VECTOR[i] << endl;
        }
    }
    Thanks for all your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Equivalent of Dos 'copy' in C ?
    By YetBo in forum C Programming
    Replies: 17
    Last Post: 11-04-2009, 10:10 PM
  2. Pointer equivalent to array notation
    By bekkilyn in forum C Programming
    Replies: 4
    Last Post: 12-06-2006, 08:22 PM
  3. Replies: 10
    Last Post: 08-17-2005, 11:17 PM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. Replies: 0
    Last Post: 11-01-2002, 11:56 AM

Tags for this Thread