Thread: help Vector c++ . . I need Help

  1. #1
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59

    help Vector c++ . . I need Help

    How to insert element in middle? because i got wrong outputs . . .plz help


    Code:
    cout<<"Enter size of vector: ";
           cin>>size;
           cout<<"Enter value of the elements: ";
           cin>>b;
          vector<int> v(size, b);
    
    cout<<"Enter element value: ";
                              cin>>in;
                        
                        vector<int>::iterator p = v.begin();
                        p+=v.size()-1;
                        v.insert(p,in);
                        for(int i = 0; i <v.size(); i++){ 
                        cout << v[i] << " ";
                        }
    plz help

  2. #2
    "PALINDROME"
    Join Date
    Nov 2010
    Posts
    59
    I 've Input 12 in size of vector and 2 for the value of elements . . so the answer is 2 2 2 2 2 2 2 2 2 2 2 2... then when i input 3 to the element value . .

    it will give me answer like this 2 2 2 2 2 2 2 2 2 2 2 3 2 . . it must be 2 2 2 2 2 2 3 2 2 2 2 2 2 . . plz help . .

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    How would you normally find the middle (hint: half) of something? It sure isn't "one from the end" at least

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting string vector to integer vector
    By CPlus in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2010, 05:43 AM
  2. including templatized vector and vector
    By -EquinoX- in forum C++ Programming
    Replies: 1
    Last Post: 11-22-2009, 04:46 AM
  3. Storing a vector inside a vector
    By csonx_p in forum C++ Programming
    Replies: 1
    Last Post: 09-14-2008, 05:15 AM
  4. Simple Question memset(vector<int>, 0, sizeof(vector<int>))
    By HyperShadow in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2007, 04:56 PM
  5. connect vector<int> and vector<char>
    By hdragon in forum C++ Programming
    Replies: 1
    Last Post: 04-27-2006, 01:16 PM