473,836 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5640
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******@comca st.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******@comca st.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************ *********@never box.com> wrote in message
news:9d******** *********@newsr ead1.news.pas.e arthlink.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
7395
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
644
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
2508
by: steflhermitte | last post by:
Dear cpp-ians, I am working with a structure struct segment { .... vector <meta_segment>::iterator it_Z; .... };
23
7437
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 in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
11
2441
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
6959
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? And yes, I know I could use the construction vector<doublev2(v1), but I'm giving an example above.
9
2112
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 size of array I need at run time without using too much memory or going over the allotted size if I choose to use this object for a different game. One idea I have is to create space * spaces = new space; then have all my accessors just convert...
4
1809
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 say: class A {
29
3442
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 make for much more readable code that is faster to write in some situations. I've not seen this feature documented anywhere
6
1821
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
0
9671
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10852
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10553
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10596
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9382
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6980
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4459
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.