Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Vectors

Vectors

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bhangie
    wrote on last edited by
    #1

    Hi there I create a vector of size(10) using a for loop to insert values into the vector for(int i = 0; i < 10; ++i) v.push_back(i + 0.1); //Display for(int i = 0; i < 10; ++i) cout << v[i] << "\n"; Only display zero's on screen, When i dont put in an initial size the vector diplays what i want. is it possible and to use v.size() rather than 10 in the for loops, but also displays zero's and warings at compile time. Am i doing something wrong! Help!:-D Regards bhangie

    Y J B 4 Replies Last reply
    0
    • B bhangie

      Hi there I create a vector of size(10) using a for loop to insert values into the vector for(int i = 0; i < 10; ++i) v.push_back(i + 0.1); //Display for(int i = 0; i < 10; ++i) cout << v[i] << "\n"; Only display zero's on screen, When i dont put in an initial size the vector diplays what i want. is it possible and to use v.size() rather than 10 in the for loops, but also displays zero's and warings at compile time. Am i doing something wrong! Help!:-D Regards bhangie

      Y Offline
      Y Offline
      YaronNir
      wrote on last edited by
      #2

      is the vector a type of std::vector or is it CArray??? look at this example for std::vector - int main( ) { using namespace std; vector v1; v1.push_back( 10 ); v1.push_back( 20 ); const int &i = v1.at( 0 ); int &j = v1.at( 1 ); cout << "The first element is " << i << endl; cout << "The second element is " << j << endl; } notice that the item is retrieved with at() function and not with the operator [] ....... do the same for CArray using GetAt() method instead of at() hope this helps Yaron Ask not what your application can do for you, Ask what you can do for your application

      1 Reply Last reply
      0
      • B bhangie

        Hi there I create a vector of size(10) using a for loop to insert values into the vector for(int i = 0; i < 10; ++i) v.push_back(i + 0.1); //Display for(int i = 0; i < 10; ++i) cout << v[i] << "\n"; Only display zero's on screen, When i dont put in an initial size the vector diplays what i want. is it possible and to use v.size() rather than 10 in the for loops, but also displays zero's and warings at compile time. Am i doing something wrong! Help!:-D Regards bhangie

        J Offline
        J Offline
        jhwurmbach
        wrote on last edited by
        #3

        With std::vector< double > v(10); you create a vector of 10 elements with the value of 0.0 Later, with v.push_back() you add elements at the end of the vector. When you display the first 10 items of the vector, you only show the zeros inserted at construction. When you let your loop run to v.size(), (make i an unsigned int, or better still, std::vector< double >::size_type) it should display all you did put into the vector. Hope this did clear things up for you


        Who is 'General Failure'? And why is he reading my harddisk?!?

        1 Reply Last reply
        0
        • B bhangie

          Hi there I create a vector of size(10) using a for loop to insert values into the vector for(int i = 0; i < 10; ++i) v.push_back(i + 0.1); //Display for(int i = 0; i < 10; ++i) cout << v[i] << "\n"; Only display zero's on screen, When i dont put in an initial size the vector diplays what i want. is it possible and to use v.size() rather than 10 in the for loops, but also displays zero's and warings at compile time. Am i doing something wrong! Help!:-D Regards bhangie

          B Offline
          B Offline
          bhangie
          wrote on last edited by
          #4

          Thanks YaronNir std::vector cleared up a few stuff ? 0.1,......up until 999.1 push_back(?)(or i + 0.1) display all contents in vector bhangie regards

          1 Reply Last reply
          0
          • B bhangie

            Hi there I create a vector of size(10) using a for loop to insert values into the vector for(int i = 0; i < 10; ++i) v.push_back(i + 0.1); //Display for(int i = 0; i < 10; ++i) cout << v[i] << "\n"; Only display zero's on screen, When i dont put in an initial size the vector diplays what i want. is it possible and to use v.size() rather than 10 in the for loops, but also displays zero's and warings at compile time. Am i doing something wrong! Help!:-D Regards bhangie

            B Offline
            B Offline
            bhangie
            wrote on last edited by
            #5

            Thanks oaks got a better better understanding now! bhangie

            J 1 Reply Last reply
            0
            • B bhangie

              Thanks oaks got a better better understanding now! bhangie

              J Offline
              J Offline
              jhwurmbach
              wrote on last edited by
              #6

              Just as an excercise - Here is how to use iterators for the display:

              #include "stdafx.h"
              #include "vector"
              #include "iostream"
              #include "string"

              int _tmain(int argc, _TCHAR* argv[])
              {
              std::vector< double > v(10);

              for(int i = 0; i < 10; ++i)
              v.push\_back(i + 0.1);
              
              //Display
              std::vector< double >::iterator it = v.begin();
              while (it != v.end())
              {
                  std::cout << \*it << "\\n";
                  ++it;
              }
              
              std::string s;
              std::cin >> s;
              

              }


              Who is 'General Failure'? And why is he reading my harddisk?!?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups