R
Ram Laxman
Hi all,
Can I assign an array to a vector if both(array and vector)
holds same data type?
Can I assign an array to a vector if both(array and vector)
holds same data type?
Ram said:Hi all,
Can I assign an array to a vector if both(array and vector)
holds same data type?
Jeff Schwab said:Ram said:Hi all,
Can I assign an array to a vector if both(array and vector)
holds same data type?
Yes, but maybe not using the syntax you expect.
#include <vector>
int main( )
{
std::vector<int> vector;
int array[ ] = { 1, 2, 3 };
vector.assign( array, array + 3 );
}
Thomas said:Or just:
#include <vector>
int main()
{
int array[] = { 1, 2, 3 };
std::vector<int> vec(array, array + sizeof(array));
Thomas said:Jeff Schwab said:Ram said:Hi all,
Can I assign an array to a vector if both(array and vector)
holds same data type?
Yes, but maybe not using the syntax you expect.
#include <vector>
int main( )
{
std::vector<int> vector;
int array[ ] = { 1, 2, 3 };
vector.assign( array, array + 3 );
}
Or just:
#include <vector>
int main()
{
int array[] = { 1, 2, 3 };
std::vector<int> vec(array, array + sizeof(array));
}
Kevin Goodsell said:#include <vector>
int main()
{
int array[] = { 1, 2, 3 };
std::vector<int> vec(array, array + sizeof(array));
This will probably not work correctly. You wanted the number of elements
in the array, not the number of bytes used to store the array.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.