what ctor for vector(size_type) items?

  • Permalink
  • submit to reddit
  • Email
  • Follow


Hi,

Using vc++ 2003, I have an unexpected behaviour:
the following statement uses the copy-ctor to create
the vector items while I expected it uses the default-ctor:

std::vector<Foo> vFoo(10);

Am I wrong?

Marco.

// Full test program

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

class Foo
{
   int m_iVal;
public:
   Foo();
   Foo(const Foo & copy);
   void Print();
};

Foo::Foo()
{
   static int i = 0;
   ++i;
   m_iVal = i;
}

Foo::Foo(const Foo & copy)
{
   static int i = 100;
   ++i;
   m_iVal = i;
}

void Foo::Print()
{
   std::cout << m_iVal << std::endl;
}

int main()
{
   std::vector<Foo> vI(10);
   std::for_each(vI.begin(), vI.end(), std::mem_fun_ref(&Foo::Print));

   return 0;
}

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply marcosegurini 7/21/2003 8:44:50 PM

See related articles to this posting

"marco_segurini" <marcosegurini@virgilio.it> wrote in message news:a33fe984.0307202248.1eee8ef1@posting.google.com...
> Hi,
>
> Using vc++ 2003, I have an unexpected behaviour:
> the following statement uses the copy-ctor to create
> the vector items while I expected it uses the default-ctor:
>
> std::vector<Foo> vFoo(10);
>
> Am I wrong?

Yes.   The ten elements are filled by copying the second initializer argument.
The constructor you are using is essentially:
    vector(size_type n, const T& value = T())

The second arg in your case is defaulted to a default constructed Foo that is copied
ten times.



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Ron 7/22/2003 12:43:41 AM


marco_segurini wrote:

 > Using vc++ 2003, I have an unexpected behaviour:
 > the following statement uses the copy-ctor to create
 > the vector items while I expected it uses the default-ctor:
 >
 > std::vector<Foo> vFoo(10);

The signature of this constructor is actually

  template < class T, class Alloc = std::allocator<T> >
  vector<T, Alloc>::vector( size_type n, const T& val = T(),
                            const Alloc& a = Alloc() );

So it default constructs one Foo (using the second
parameter's default argument) and copies it ten times.

--
Richard Smith

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Richard 7/23/2003 11:25:41 AM

"Ron Natalie" <ron@sensor.com> wrote in message news:3f1c531d$0$39702

> The constructor you are using is essentially:
>     vector(size_type n, const T& value = T())
>
> The second arg in your case is defaulted to a default constructed Foo that
is copied
> ten times.

Good point.  Class vector can be used to store objects with no default
constructor.

struct Foo {
   explicit Foo(int);
};

int main() {
   vector<Foo> v(10, Foo(10));
   v.push_back(Foo(11));
}

--
+++++++++++
Siemel Naran


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Siemel 7/23/2003 11:14:05 PM
comp.lang.c++.moderated 10583 articles. 6 followers. Post

3 Replies
131 Views

Similar Articles

[PageSpeed] 47


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Remove an item from a vector and write to file the contents of the Vector
Hi all, I am currently developing a java application that has to deal with it reading ina CSV file and populating the data that is inside the CSV file into a vector and then places what is inside the Vector into a JList. When the user selects an item from the JList and then they press the delete button the item that they selected would get removed from the JList. Once the item has been removed from the JList I then want the application to write the information that is left inside the vector to a file without it having any commas or square brackets etc. I am fairly new to Java and therefore ...

vector::size_type
Is std::vector<T>::size_type guaranteed to be the same type as std::vector<U>::size_type? To be more explicit, given void f(T, U); and std::vector<T> vt; std::vector<U> vu; which have the same size, can I write for (std::vector<T>::size_type i = 0; i < vt.size(); ++i) f(vt[i], vu[i]); or should I write for (std::vector<T>::size_type i = 0; i < vt.size(); ++i) f(vt[i], vu[static_cast<std::vector<U>::size_type>(i)]); Thank you. On 2007-10-09 23:18, t.hall@liv.ac.uk wrote: > Is std::vector<T>::size_type guaranteed to be t...

explicit ctor in vector
In the standard library vector, the ctor explicit vector::vector(size_type __n) is declared as explicit. I am unable to get the reason for it being declared as explicit. Kindly clarify with an example. Does the standard require that this ctor should be declared explicit ? Also, please let me know if there are any guidelines for identifying which 'one argument ctors' need to be declared as explicit. Thanks V.Subramanian subramanian100in@yahoo.com, India wrote: > In the standard library vector, the ctor > explicit vector::vector(size_type __n) > > is declared as explici...

Problem with deleting vector items
Hi. I got a problem with deleting items of a vector. I did it like this: void THIS::bashDelPerson() { cout << "Bitte Suchstring eingeben: "; char search[128]; cin >> search; vector<Person>::iterator iter; iter = persons.begin(); Person* person; while ( iter != persons.end() ) { if ( strstr(iter->getFirstName(), search) != 0 ) { persons.erase(iter); } cout << "Moep\n"; if(iter != persons.end()){iter++;} } } But this implementation only works if the Person, which should be deleted is the last one. If it is followed by another...

Directly Modify Vector Item
Is it possible to directly manipulate an item within a Vector (or anyother AbstractCollection).Basically, I retrieve and manipulate vectors in the following way:Vector myVec = new Vector();// populate...for (int i = 0; i < myVec.size; i++) { String myStr = (String)myVec.get(i); myStr = "String " + i; myVec.setElementAt(myStr, i);}While arrays can simply modify directly:for (int i = 0; i < myVec.size; i++) { myArray[i] = "String " + i;}The first method seems cumbersome and inefficient, especially whenusing a class other than a String (one with many modifications to...

std::vector ctor guarantee.
I'd like to know that when I use the InputIterator, InputIterator version of the vector ctor, or that particular insert overload, is it guaranteed that reserve will be called, and the copes will be made only once. Particularly, something like this: vector<int> v (23, 45); vector<int> v1 (v.begin(), v.end()); //Any guarantee that elements will be copied only once. ie: Only 23 copies will be made when this statament executes? Or do I have to manually call reserve before a call to insert which has a similar signature? Also one more thing. If I have a type such as int*, then do ...

How to compare items in a column vector?
Hello, Let's say I have a 6x4 matrix looking like: 1 6 56 45 2 4 7 18 3 91 56 4 2 6 56 45 1 91 56 4 3 4 7 18 The first column represents conditions 1, 2, and 3, where each condition is repeated twice. The numbers in the last 3 columns are random. What I wish to do is take the rows for each condition and determine the mean across the repetitions. How can I compare each item in the first cloumn vector to see if they equal and then take the mean across the rows that have an equal value in that first cell? Thanks, it will be greatly appreciated!...

Remove items from a stl vector
How can I remove items from a stl vector if it exists on another vector? In Java, I can do this: vector v1 = new Vector(); // populate v1 vector v2 = new Vector(); // populate v2 v1.removeAll(v2); how can I do that in C++? Do I have to write my own functor and pass that to remove_if() algorithm? silverburgh.meryl@gmail.com wrote: > How can I remove items from a stl vector if it exists on another > vector? Which one do you designate 'it'? The item or the vector? > In Java, I can do this: > vector v1 = new Vector(); > // populate v1 > > vector v2 = new Vect...

question on std::vector default ctor
In _The C++ Standard Library_ by Josuttis, page 149, is the assertion that std::vector<T> v(5); calls the default ctor of T 5 times. I wrote a test case for this using gcc 3.2, and the default ctor was called only once. Which of the following is true? 1) Josuttis was mistaken 2) The standard has changed 3) This is a gcc 3.2 bug (optimization?) 4) It's due to some other reason I haven't thought of TIA (test program follows) // vectorsize.cpp -- example of sizing a vector #include <vector> using std::vector; #include <iostream> using std::cout; using std::endl;...