Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8418055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:12:48+00:00 2026-06-10T02:12:48+00:00

I like vectors, and generally use them over arrays. For that reason I created

  • 0

I like vectors, and generally use them over arrays. For that reason I created a templated variadic function to initialize vectors (included below).

Header (.h):

template <typename T>
vector<T> initVector(const int argCount, T first, ...);

Source (.hpp):

template <typename T>
vector<T> initVector(const int argCount, T first, ...) {
    vector<T> retVec;
    retVec.resize(argCount);

    if(argCount < 1) { ... }

    retVec[0] = first;

    va_list valist;
    va_start(valist, first);
    for(int i = 0; i < argCount-1; i++) { retVec[i+1] = va_arg(valist, T); }
    va_end(valist);

    return retVec;
}

It works great for most types (e.g. int, double…), but not for strings—as the compiler interprets them as ‘const char *’, thus

vector<string> strvec = initVector(2, "string one", "string two");

gives me the error:

error: conversion from ‘std::vector<const char*, std::allocator<const char*> >’ to non-scalar type ‘std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >’ requested

Is there any way to get the string arguments to be interpreted as strings without having to cast each one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:12:49+00:00Added an answer on June 10, 2026 at 2:12 am

    Because the type of the constant "string one" is const char* and not std::string there needs to be a conversion. va_arg can not make this conversion, so we need a second template argument:

    template <typename VecT, typename EleT>
    std::vector<VecT> init_vector(const size_t nargs, EleT first, ...) {
        std::vector<VecT> result;
        result.reserve(nargs);
    
        if (nargs == 0) {
            return result;
        }
    
        result.push_back(first);
    
        if (nargs == 1) {
            return result;
        }
    
        va_list valist;
        va_start(valist, first);
    
        for (int i = 1; i < nargs; ++i) {
            result.push_back(VecT(va_arg(valist, EleT)));
        }
    
        va_end(valist);
    
        return result;
    }
    
    std::vector<std::string> = init_vector<std::string>(2, "string one", "string two")
    

    Note that I made some changes, most notably change resize to reserve, to prevent unnecessary objects from being created.


    You can also simply use this (no risk of having number of elements messed up, and type safe):

    const char *args[] = {"string one" , "string two"};
    std::vector<std::string> strvec(args, args + sizeof(args)/sizeof(args[0]))
    

    Or use C++11 initializer lists:

    std::vector<std::string> strvec = {"string one" , "string two"};
    

    For fun I made this little thing that is even neater and safer, but doesn’t generalize into an arbitrary amount of arguments. It works by overloading. Here are the first three overloads and example usage:

    template<class C>
    inline C init_container() {
        return C();
    }
    
    template<class C, class T>
    inline C init_container(T arg0) {
        const T args[1] = {arg0};
        return C(args, args + 1);
    }
    
    template<class C, class T>
    inline C init_container(T arg0, T arg1) {
        const T args[2] = {arg0, arg1};
        return C(args, args + 2);
    }
    
    std::vector<std::string> vec =
        init_container< std::vector<std::string> >("hello", "world");
    

    A full header (for up to 100 arguments) can be downloaded here: https://gist.github.com/3419369 .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to scan a variables that form vectors from white space delimited text
In MATLAB, I'd like to apply a function to every pair of column vectors
I generally use vectors/arraylists , hashmaps/treemaps, and other java collections interchangeably, with exception of
I use vectors a lot in my programming, and generally to traverse a vector
I like vectors a lot. They're nifty and fast. But I know this thing
I have been struggling to think of some decent uses for things like vectors
I have lots of vectors like this one below, very sparse, lots of 'NaN'.
I have two vectors, and I would like to fill the first one with
I have two vectors, v1 and v2. I would like to find any of
So I have two vectors of data points, and I would like to make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.