Defining a list (vector) with an array
I have a class email, there is a "bcc" parameter in its constructor. Actual list of emails to copy. The number of these letters has been fixed, and later I have the opportunity to expand this list.
//constructor prototype
Email::Email(vector<string> bcc)
So, I want to use vector or list type and push_back () function for this. How can I create a new instance with an email address?
I need an ad with a definition for my list.
I found this definition with an iterator for an integer type:
int myints[] = {16,2,77,29};
Email myEmail(vector<int> (myints, myints + sizeof(myints) / sizeof(int) ));
but its not very user friendly and i need it with strings.
Is there something like this?
Email myEmail(vector<string> ("first","second","third"));
a source to share
Besides initializing the C ++ 0x list, there is a Boost.Assign library that should do similar things.
a source to share
If you have C ++ 0x you can make a vector {"first", "second", "third"}. Also, you will need to create a new vector in the area somewhere and manually click on each one you want and then plot.
Also, you should really take this vector from the link, it's really quite big.
You should use std :: vector unless you know that you will need to insert elements in the middle and not the end.
a source to share
If you don't C++0x
, you don't have access to initializers. Could you please add a constructor that accepts any old iterator, namely:
#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;
}
Of course, there is no reason you can't have ctor
that takes const BccType&
( typedef
ed for brevity and maintainability) additionally. Note that I suggest passing this by link to save a copy std::vector
twice.
a source to share