Thread: Assigning Multiple Values to Array/ Vector

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    127

    Assigning Multiple Values to Array/ Vector

    Hi,

    Say if, like in the following example, you want to check if a variable has one of two sets of values. Is there a way of doing this, but without writing out "a1 = 12, a2 = ...." etc.?

    I'm thinking you could do it with two arrays- one for the first set of values then another for the second set. Then you would just assign one of those arrays to a vector and check if c is any of the values in the vector. But I'm wondering, would it be possible to do this with using only 1 array or vector? Something like:

    Code:
    vector<int> v1;
    if (b == 1) 
        v1 = {12, 15, 11, 17, 18 };
    if (b == 2) 
        v1 = {21, 25, 27, 26, 28 };
    Or is there any other more efficient way to do the program?

    Thanks.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int a1, a2, a3, a4, a5, b, c;
        cout << "press 1 for set 1 and 2 for set 2";
        cin >> b;
        if (b == 1)
        {
            a1 = 12;
            a2 = 15;
            a3 = 11;
            a4 = 17;
            a5 = 18;
        }
        else if (b == 2)
        {
            a1 = 21;
            a2 = 25;
            a3 = 27;
            a4 = 26;
            a5 = 28;
        }
        cout << "Enter a number to check if it's in the set selected";
        cin >> c;
        if (c == a1 || c == a2 || c == a3 || c == a4 || c == a5)
            cout << "You entered a value that is in set " << b;
        else
            cout << "Number not in set " << b;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Your idea of using a vector is a good one. You can then just use the find() function in the vector to see if the number is in your set.

    You may want to consider doing something like:
    Code:
    vector<int> set1;
    vector<int> set2;
    // fill vectors with set data
    
    cin >> b;
    
    // Assign chosenSet to the set the user selected.  I didn't do error checking to see if b is within bounds (not ideal)
    vector<int>& chosenSet = (b == 1) ? set1 : set2;
    
    // Now check if the number the user entered is in the chosenSet vector

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    127
    Interesting. I've never seen ? and : used like that before, but I looked them up and they seem useful. Thanks for the example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2008, 10:25 AM
  2. how do i read 2 consecutive values in vector and compute?
    By dalearyous in forum C++ Programming
    Replies: 8
    Last Post: 03-30-2006, 04:22 PM
  3. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Writing multiple values to an array
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-06-2002, 03:27 PM