Thread: make a vector constant?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    308

    make a vector constant?

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    void test(vector <int*> v)
    {
    	for (int i = 0; i < v.size(); i++)
    	{
    		if (v[0] == v[i])
    		{
    			cout << v[i] << endl;
    		}
    		else
    		{
    			cout << "ugly address man.\n";
    		}
    	}
    }
    
    int main()
    {
    	int j = 2;
    
    	vector <int*> v; // size of the vector starts at 0
    
    	for (int i = 0; i < 2; i++)
    	{
    		if (i != 1)
    		{
    			v.push_back(&i); // v now has 1 element
    			test(v);
    		}
    		else
    		{
    			v.push_back(&j); // v now has 1 element
    			//v[1] = v[0];
    			test(v);
    		}
    	}
    }
    you see line 36 //v[1] = v[0]; change the element 1 to look like element 0 when uncommented? i want to know how to make the element 1 constant so it can't be changed. and do this for every element in the vector, please. i googled how but i didn't find anything.
    Last edited by jeremy duncan; 02-10-2021 at 01:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character arrays - constant values in a constant expression
    By hewlettuser in forum C++ Programming
    Replies: 2
    Last Post: 04-07-2017, 04:00 AM
  2. Replies: 9
    Last Post: 09-22-2013, 07:00 AM
  3. Replies: 45
    Last Post: 04-02-2008, 09:10 AM
  4. Why make a constant a constant again?
    By Overworked_PhD in forum C Programming
    Replies: 3
    Last Post: 11-03-2007, 06:57 PM
  5. Newbie: how do you make a char a constant?
    By Bob++ in forum C++ Programming
    Replies: 4
    Last Post: 12-21-2006, 06:17 PM

Tags for this Thread