473,387 Members | 1,892 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,387 software developers and data experts.

Must elements of a std::vector be contiguous?

2
I read in
http://www.open-std.org/jtc1/sc22/wg...efects.html#69
they proposed:
The elements of a vector are stored contiguously, meaning that if v is a vector<T, Allocator> where T is some type other than bool, then it obeys the identity
Expand|Select|Wrap|Line Numbers
  1. &v[n] == &v[0] + n for all 0 <= n < v.size()
  2.  
My question is: does the above implies the:
Expand|Select|Wrap|Line Numbers
  1. (Type *) &v<char>[n]==(Type *)(&v<char>[0] + n*sizeof (Type));
  2.  
?
where v is a std::vector of char, and Type is in general a simple structured type.
Thanks and sorry for my bad english
Jul 15 '09 #1
3 1984
Banfa
9,065 Expert Mod 8TB
No, in fact it implies precisely not that as long as sizeof(Type) != 1.

for

vector<char> v;

as you have already stated

&v[n] == &v[0] + n

for for sizeof(Type) != 1 then n != n * sizeof(Type)

so

&v[n] != &v[0] + n*sizeof (Type);

However elements of a vector must be contiguous, that is what the standard says and that is true.
Jul 15 '09 #2
MacApp
2
Thanks for the fast reply, and you are right... i made a mistake in my question sorry.

I meant:
Expand|Select|Wrap|Line Numbers
  1. &v[n] == &v[0] + n;
  2.  
implies:
Expand|Select|Wrap|Line Numbers
  1. (Type *) &v<char>[n*sizeof (Type)]==(Type *)(&v<char>[0] + n*sizeof (Type));
  2.  
?

where, in pratic v<char> is used as a C buffer of chars containing infact an array of Type.
Jul 15 '09 #3
Banfa
9,065 Expert Mod 8TB
Well in that case you are correct. However I wouldn't necessarily call that good programing practice particularly in a C++ program. On the whole if the object has a type it should be held as that type.
Jul 15 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Newsgroup - Ann | last post by:
Hi: I saw the following codes from the FAQ about the contiguous storage of vector. I am just wondering how does the implementation of the <vector> guarantee the contiguous? What if a vector v...
27
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
8
by: Ravi | last post by:
Is a std::vector *guaranteed* to be contiguous in memory? Bjarne Stroustrup says it takes constant time to access a vector element and that implies contiguous storage but I just wanted to...
6
by: Jason Heyes | last post by:
What is a good way of removing elements from std::vector so that the elements removed satisfy a predicate and end up stored in another std::vector. It seems as though the algorithm std::remove_if...
1
by: Alex Vinokur | last post by:
Testsuites "Comparative Performance Measurement. Reading file into string" at http://groups.google.com/group/perfo/msg/8273f4d1a05cfbd1 http://groups.google.com/group/sources/msg/27a9b6f91239c909...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
6
by: slyi | last post by:
Is it ok to assume that the following assertion is valid for all implementations of std::vector?: std::vector<T> v(10); T* p = &v; for (size_t n=0; n < v.size(); n++) assert( p+n == &v ); ...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
4
by: Bobrick | last post by:
Hi. I'm in the process of making a GUI for a function someone else wrote, and i've come across a type i'm unfamiliar with, namely "std::vector<unsigned char>". I need to get the contents of this...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...

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.