• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
BaneTrapper

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

15 posts in this topic

Hello.

I have a case as such

void func(std::vector<std::string>> VS);

And i need to call this function with vector that i initialize in the function call.

As such:

func(std::vector<std::string>{std::string("parameter1"), std::string("parameter2")});

Off course this example does not work, but it shows what i require.

The function can take from 1 to X strings passed intro it, and i do not want to pass each string separated, it needs to handle (1,2,3... 66,67...) strings.

What are workarounds? i didn't find one myself.

0

Share this post


Link to post
Share on other sites

Why exactly do you "need" this to be precisely this way? There are canonical solutions that accomplish the exact same thing but without cramming a bunch of stuff into one function call's parameter list.

0

Share this post


Link to post
Share on other sites

I have function

void SetForInput(editor::EditorInputType EIT, editor::EditorInputExecuteTypes EIET, std::vector<std::string> & VS);
void SetForInput(editor::EditorInputType EIT, int inputAmount, editor::EditorInputExecuteTypes EIET);

 

This is what i end up doing

SetForInput(editor::EITIntInt, 2, editor::EIETNewMap);//int int = 2 input
editorInputHints.push_back("Input Map Size X:");//Add as many hints as passed value intro SetForInput, else crash
editorInputHints.push_back("Input Map Size Y:");

Id like to put it all in one function call and make it all simple.

In case i go after a month back and forget to add strings intro editorInputHints, i will have bad time, but if it is in function call its simply gonna note me.

0

Share this post


Link to post
Share on other sites
Why not just have a function for each type of widget you want to create, with the appropriate number of parameters? You can then use the wrapper function directly, and internally call your other code with the correct data (although I wouldn't keep that interface personally).
0

Share this post


Link to post
Share on other sites

Why not just have a function for each type of widget you want to create, with the appropriate number of parameters? You can then use the wrapper function directly, and internally call your other code with the correct data (although I wouldn't keep that interface personally).

That is exactly what i am trying to evade.

 

I got same code for each case... I could have one base function that has 6 lines of code, and all other functions could call it, but it gets to spaghetti code unneedly.

If there is one string passed or 100 string passed, the function is still 6 lines of code. i hate to make allot of functions that take different amount of string parameter and then call the 6 base lines of code, i find it just waste.

Possibly i didn't understand what you meant.

 

 

 

 

If you just want to pass many strings to function and you are using C++11, the easiest approach is to use initializer list.

void func(std::vector<std::string> const &_vs);

int main()
{
    func({"one", "two", "three"});
    return 0;
}

I already tried that out

void func(std::vector<std::string> const vS)
{
    std::cout<<vS[0]<<std::endl;
}

int main()
{

    func({"Hello", "Hy"});

compiler error

Ignore that its on 84 line, its the func() call made.

1>c:\c++\projects\test\test\main.cpp(84): error C2143: syntax error : missing ')' before '{'
1>c:\c++\projects\test\test\main.cpp(84): error C2660: 'func' : function does not take 0 arguments
1>c:\c++\projects\test\test\main.cpp(84): error C2143: syntax error : missing ';' before '{'
1>c:\c++\projects\test\test\main.cpp(84): error C2143: syntax error : missing ';' before '}'
1>c:\c++\projects\test\test\main.cpp(84): error C2059: syntax error : ')'

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

I have c++11 function as

for(int & i : vecOfInt)
{...}
Edited by BaneTrapper
0

Share this post


Link to post
Share on other sites
One option is to use something like this:
template <typename T>
struct make_vector
{
    make_vector() { }
 
    make_vector(T const &_element)
    {
        data.push_back(_element);
    }
 
    make_vector& operator()(T const &_element)
    {
        data.push_back(_element);
        return *this;
    }
 
    operator std::vector<T>()
    {
        return data;
    }
 
    std::vector<T> data;
};
 
int main()
{
    func(make_vector<std::string>("one")("two")("three"));
    return 0;
}
It's quite ugly workaround but is also pretty easy to replace it once the initializer list will be available to you. Edited by Consensus
0

Share this post


Link to post
Share on other sites
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.

0

Share this post


Link to post
Share on other sites

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.

Edited by JohnnyCode
0

Share this post


Link to post
Share on other sites

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.

Edited by Juliean
1

Share this post


Link to post
Share on other sites

 

 

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.

1

Share this post


Link to post
Share on other sites

 

 

 

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!

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0