473,383 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Can I assign safely an array to vector?

Hi all,
Can I assign an array to a vector if both(array and vector)
holds same data type?
Jul 22 '05 #1
6 5616
Ram Laxman wrote:
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 );
}
Jul 22 '05 #2
Ram Laxman wrote:
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 );
}
Jul 22 '05 #3

"Jeff Schwab" <je******@comcast.net> wrote:
Ram Laxman wrote:
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));
}

Best regards,

Tom
Jul 22 '05 #4
Thomas Tutone wrote:
Or just:

#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.

int array[] = { 1, 2, 3 };
int size = sizeof array / sizeof *array;
std::vector<int> vec(array, array + size);

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #5
Thomas Tutone wrote:
"Jeff Schwab" <je******@comcast.net> wrote:
Ram Laxman wrote:
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));
}


That's not assignment, it's initialization.
Jul 22 '05 #6

"Kevin Goodsell" <us*********************@neverbox.com> wrote in message
news:9d*****************@newsread1.news.pas.earthl ink.net...
#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.


Oops. Absolutely right.

Best regards,

Tom
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: scooter | last post by:
what happens memory wise with this scenerio: std::vector<int> vecInt(20, 999); VecInt.assign(20, 0); do all the 999 values get overwritten or does the vector reassign the memory lso?
6
by: Ram Laxman | last post by:
Hi all, Can I assign an array to a vector if both(array and vector) holds same data type?
4
by: steflhermitte | last post by:
Dear cpp-ians, I am working with a structure struct segment { .... vector <meta_segment>::iterator it_Z; .... };
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
11
by: nandor.sieben | last post by:
I am trying to replace template < class T void set2vector (const set < T &s, vector < T &v) { typename set < T >::iterator it; for (it = s.begin (); it != s.end (); it++) { v.push_back (*it);...
4
by: Chris Roth | last post by:
vector<doublev1(5,1); vector<doublev2; v2 = v1; // 1 v2.assign(v1.begin(),v1.end()); // 2 Are 1 and 2 the same, or are their subtle differences between them. Which is preferable, if either? ...
9
by: JoeC | last post by:
I am crating a new version of my map game and my map will be a 2d array. I had problems trying to create a 2d array dynamically, in fact C++ won't let me do it. My question is how to create the...
4
by: tech | last post by:
Hi, I need to pass a block of say 320 bytes memory between some classes which do various processing on it. The app needs to be quick so i can't keep copying. The simplest way is via pointer...
29
by: stephen b | last post by:
Hi all, personally I'd love to be able to do something like this: vector<intv; v.assign(1, 2, 5, 9, 8, 7) etc without having to manually add elements by doing v = 1, v = 2 .. etc. it would...
6
by: remlostime | last post by:
now, i write some code code1: int a; int main(){} code2: vector<inta(10000000); int main(){} after using g++ compile, and run it, code1 is broken, but code2 runs
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.