views:

170

answers:

3

I have Class Email, there is parameter "bcc" in her constructor. Its actually list of emails for copies. There is no fixed number of these emails and later i have to have possibility to extend this list.

//constructor prototype
Email::Email(vector<string> bcc)

So i want to use type vector or list for that and function push_back(). How can i make a new instance with bcc emails?

I need actually declaration with definition for my list.

I've found this definition with iterator for integer type:

int myints[] = {16,2,77,29};
Email myEmail(vector<int> (myints, myints + sizeof(myints) / sizeof(int) ));

, but its not very user friend and i need it with strings.

Is there something like this?

Email myEmail(vector<string> ("first","second","third"));
+2  A: 

If you have C++0x, you can do vector { "first", "second", "third" }. Else, you will have to create a new vector in scope somewhere and manually push on each that you want, then construct.

Also, you should really take that vector by reference, it's really quite large.

You should use a std::vector unless you know that you will need to insert items into the middle, not on the end.

DeadMG
Even if you need to insert items in the middle stick with a `vector` (or a `deque`) unless you identify it as a bottleneck. For less than a few dozens elements, it'll perform better, after that it depends on your architecture.
Matthieu M.
Its not program for intel platform, so i cant use any features or big library (i have no space for that). I am lookig for a standard solution.
Meloun
+3  A: 

Apart from C++0x list-initialization, there is the Boost.Assign library which should do similar things.

jpalecek
+1  A: 

If you're not using C++0x then you have no access to initialisers. Could you add a constructor that takes any old iterator, viz:

#include <vector>
#include <list>
#include <iostream>

struct Email
{
    typedef std::vector<std::string> BccType;
    BccType m_bcc;

    template <typename T>
    Email(const T& iter, const T& end)
        :m_bcc(iter, end)
    {
    }

    // Purely here for illustrative purposes...
    void display()
    {
        std::cerr << m_bcc.size() << " addresses..." << std::endl;
        for (BccType::iterator iter = m_bcc.begin(), iterEnd = m_bcc.end(); iter != iterEnd; ++iter)
        {
            std::cerr << *iter << std::endl;
        }
    }
};

int main(int, char*[])
{
    // Plain old char* array...
    const char* bcc[] = {"Jeff", "Joel", "Larry", "Brin"};
    const size_t count = sizeof bcc / sizeof bcc[0];
    Email email(&bcc[0], bcc + count);
    email.display();

    // STL container variation...
    std::list<std::string> names;
    names.push_back("Bill");
    names.push_back("Steve");
    Email reply(names.begin(), names.end());
    reply.display();
    return 0;
}

There is, of course, no reason why you can't have a ctor that takes const BccType& (typedefed for brevity and maintainability) additionally. Note that I suggest passing this by reference to save copying the std::vector twice.

Johnsyweb