Function that takes unknown amount of strings, how do i vector?

Started by
14 comments, last by BaneTrapper 9 years, 10 months ago

One option is to use something like this...
It's quite ugly workaround but is also pretty easy to replace it once the initializer list will be available to you.

Otherwise known as boost::assign::list_of().

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Advertisement

typedef std::vector<std::string> StringList;

StringList &operator<<(StringList &a, const std::string s)
{
    a.push_back(s);
    return a;
}

void func(const StringList &s);

void f()
{
    func(StringList() << "one" << "two" << "three");
}

This sort of thing is used a lot in Qt, although QVector and QList have built-in << operators. The above should work in principle though, but might be better to wrap std::vector<std::string> inside a StringList class.

there is also a rather native solution for strings in a string, by sealing strings upon a common denominator

int vrbcnt=GetVerbs("|go|done|big");

you can proceduraly create the parameter.


there is also a rather native solution for strings in a string, by sealing strings upon a common denominator

Possible, but I wouldn't recommend it, unless there is a parsing solution that I'm unaware of. If you have to parse it manually, its a pain compared to the vector


void function(const std::vector<std::string>& vStrings)
{
    for(auto& str : vStrings)
    {
        // do stuff
    }
}

void function(const std::string& strValues)
{
    const auto strCopy = strValues; // we can't modify the incoming string, so copy is necessary, unless we want to store current-position, which makes things even more complicated
    
    auto seperator = strCopy.find("|");
    while(seperator != std::string::npos)
    {
        const auto nextSeperator = strCopy.find("|");
        const auto strValue = strCopy.substr(seperator+1, nextSeperator - (seperator + 1));
        
        // do stuff with value
        
        strCopy.erase(seperator);
        
        seperator = strCopy.find("|");
    }

    if(!strCopy.empty())
    {
        // do stuff in case eigther there is no seperator or there is still something left - dublicate code incoming!
    }
}

I cringe everytime I even have to think about writing something like that... I do believe that there is some logic error in my code though that fixing would require even more code (it is just an example, sue me ;) ).

EDIT: In a less serious manner, this reminds me of Stringly typed (7.), and I personally (seriously again) would not recommend to fall back to parsing values in and out of strings unless you absolutely have to.

Possible i don't have latest c++11?

I'm guessing you're using Visual Studio. VS <2013 doesn't have initializer lists.

I am out dated blink.png... 99$ for 2013 upgrade, too broke ATM for that.

I will make do with horrible syntax.

You can get the Visual Studio 2013 Expess edition for free if it fits your needs. You only have to register your email after 30 days to continue free usage for unlimited period.

Possible i don't have latest c++11?

I'm guessing you're using Visual Studio. VS <2013 doesn't have initializer lists.

I am out dated blink.png... 99$ for 2013 upgrade, too broke ATM for that.

I will make do with horrible syntax.

You can get the Visual Studio 2013 Expess edition for free if it fits your needs. You only have to register your email after 30 days to continue free usage for unlimited period.

laugh.png thx!

This topic is closed to new replies.

Advertisement