-1

I had some problem with string arrays C++ array size different result I got the advice to use vectors instead of arrays. But this works:

#include "stdafx.h"
#include <string>
#include <iostream>
#include <vector>

using namespace std;

vector<int> a (1,2);

void test(vector<int> a)
{
    cout << a.size(); 
}
int _tmain(int argc, _TCHAR* argv[])
{
     test(a);

    return 0;
}

But this wont:

vector<string> a ("one", "two");

void test(vector<string> a)
{
    cout << a.size(); 
}
int _tmain(int argc, _TCHAR* argv[])
{
     test(a);

    return 0;
}

error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'const char' to 'const std::basic_string<_Elem,_Traits,_Ax> &'

I dont get whats going wrong.

Community
  • 1
  • 1
user1686999
  • 3
  • 1
  • 2
  • Something should've seemed fishy when the first printed a size of 1. – chris Sep 21 '12 at 17:58
  • 1
    Just for future, in case you don't realize it. If you design a function that works on *large* objects such as containers it is not a good idea to pass them by value. Now you are making a copy of whole `a` to call `size` on it. `void test(const vector& a)` would be much better alternative. – luk32 Sep 21 '12 at 18:02

4 Answers4

2

The first one is calling a constructor (N, X) that creates N elements, each with a value of X, so you end up with one 2.

There is no match for the second constructor, as none take two const char * or similar.

Use curlies instead, as there is a match for an initializer list (at least in C++11):

std::vector<int> v{1, 2}; //contains a 1 and a 2
std::vector<std::string> v2{"hi", "bye"}; //contains "hi" and "bye"

In C++03, you can instead do this:

int vecInit[] = {1, 2, 3, 4};
std::vector<int> vec(vecInit, vecInit + sizeof vecInit / sizeof vecInit[0]);

You'll end up copying the items from the array into the vector to initialize it because you utilize the constructor that takes two iterators, of which pointers are random-access.

chris
  • 60,560
  • 13
  • 143
  • 205
2

The constructor for vector doesn't take a list of items, it takes a single item and a count.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1

std::vector has several constructors. One of them expects number of elements as a first parameter and elements value as the second one.

In case of vector<int> a (1,2) you initialized vector a with 1 element which value is 2.

In case of vector<string> a ("one", "two"); compiler cannot convert first parameter to int (or to any other types expected as the first parameter of other constructors).

As a workaround you can try something like:

std::string ch[] = {"one", "two"};
std::vector<std::string> a (ch, ch + _countof(ch));

This will fill a with two strings: "one" and "two"

alexkorep
  • 531
  • 2
  • 9
0

the first parameter of the constructors of vector is the number of elements, and the second is the value of these elements.

vector<int>a (1,2) means 1 element which value is 2, but there's no constructors of vector matching vector<string> a("one","two").

Littm
  • 4,923
  • 4
  • 30
  • 38
xchang
  • 1