473,383 Members | 1,748 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.

Pointers as iterators; vector<string> representation...


1. If I pass pointers (char*) as iterators to an STL algorithm and the
return value is an iterator, can I convert that iterator to a pointer? If
yes, how?
2. What is the internal representation of vector<string>? Will the vector
contain the string objects or will it contain pointers/references to the
string objects? The reason I ask is that it is not clear to me how the
v.reserve() and &v[0] operations would work for a vector<string>.
Nov 7 '08 #1
9 3788
barcaroller wrote:
1. If I pass pointers (char*) as iterators to an STL algorithm and the
return value is an iterator, can I convert that iterator to a pointer? If
yes, how?
For most of the standard algorithms, the return value type is the same
as the one you pass. It's already a pointer, no need to convert. Test
it, you'll see.
2. What is the internal representation of vector<string>?
Implementation-defined.
Will the vector
contain the string objects or will it contain pointers/references to the
string objects? The reason I ask is that it is not clear to me how the
v.reserve() and &v[0] operations would work for a vector<string>.
The vector will contain a dynamic array of 'string' objects. The
'reserve' operation allocates the array capable of containing future
'string' objects (which will be constructed using 'placement new', most
likely). Taking the address of v[0] (which returns the reference to the
very first object in the inner array) will give you the starting address
of the inner array. What's there to [not] understand? Now, you
probably need to understand how 'string' works, as well. Do you?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 7 '08 #2

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:gf**********@news.datemas.de...
The vector will contain a dynamic array of 'string' objects. The
'reserve' operation allocates the array capable of containing future
'string' objects (which will be constructed using 'placement new', most
likely). Taking the address of v[0] (which returns the reference to the
very first object in the inner array) will give you the starting address
of the inner array. What's there to [not] understand? Now, you probably
need to understand how 'string' works, as well. Do you?
If the vector "contains" the strings, then reserving ahead of time is not
useful for optimization because the vector would still not know what the
size of the strings are until they are actually inserted.

If, however, the vector contains only pointers/references to the strings,
reserve() would still not be useful.

Nov 8 '08 #3
barcaroller wrote:
If the vector "contains" the strings, then reserving ahead of time is not
useful for optimization because the vector would still not know what the
size of the strings are until they are actually inserted.
Yes, we always do know the "size of the strings" in advance. The
'std::vector' object in this case contains 'std::string' objects. And
the size of the latter is always pre-determined and known at compile
time. This is the only size 'std::vector' needs to know about.

In general case it is the 'std::string' object itself that contains the
pointer to the controlled sequence (the actual "string"). The length of
the sequence can vary (it is allocated independently), but the size of
the 'std::string' object itself is always fixed and does not depend in
any way on the current length of the controlled sequence.

In other words, the actual "strings" (character sequences) are stored in
'std::vector<std::string>' by pointers, but it has absolutely nothing to
to with the 'std::vector' itself. This is an internal issue of
'std::string'. 'std::vector' doesn't know about it and doesn't care
about it.

Reserving 'std::vector<std::string>' ahead of time is, of course, useful
- it prevents unnecessary reallocations of 'std::vector' when adding
extra 'std::string' objects to the 'std::vector'.
If, however, the vector contains only pointers/references to the strings,
reserve() would still not be useful.
You seem to be caught in a terminological mix-up with regards to what is
a "string" and what the word "stores" means in this case.

But in any case, 'reserve()' is still useful (as described above).
Unless, again, you give the word "useful" some meaning that is not
immediately clear to me.

--
Best regards,
Andrey Tarasevich
Nov 8 '08 #4
barcaroller wrote:
1. If I pass pointers (char*) as iterators to an STL algorithm and the
return value is an iterator, can I convert that iterator to a pointer? If
yes, how?
Can you illustrate what you are talking about with an example? Normally
the return value is an iterator of the same kind as the one (the ones)
that was passed in. If you pass 'char*'s, you get back a 'char*'. No
need to convert anything.

--
Best regards,
Andrey Tarasevich
Nov 8 '08 #5
On Nov 8, 1:42*am, "barcaroller" <barcarol...@music.netwrote:
"Victor Bazarov" <v.Abaza...@comAcast.netwrote in message
news:gf**********@news.datemas.de...
The vector will contain a dynamic array of 'string' objects.
*The 'reserve' operation allocates the array capable of
containing future 'string' objects (which will be
constructed using 'placement new', most likely). *Taking the
address of v[0] (which returns the reference to the very
first object in the inner array) will give you the starting
address of the inner array. *What's there to [not]
understand? *Now, you probably need to understand how
'string' works, as well. *Do you?
If the vector "contains" the strings, then reserving ahead of
time is not useful for optimization because the vector would
still not know what the size of the strings are until they are
actually inserted.
That depends on the implementation of std::string. The sizeof
(as opposed to the size of) an object is a compile time
constant; std::string needs an additional indirection to handle
variable length, but this is hidden in std::string; std::vector
doesn't no beans about it. And depending on the implementation,
it may not always have to do a deep copy, or it may not need the
extra indirection if the strings are short enough.
If, however, the vector contains only pointers/references to
the strings, reserve() would still not be useful.
Oh yes it is. Perhaps even more so. Increasing the capacity of
a vector means copying member objects. If std::string is
systematically doing a deep copy (most do, at least if the
strings are longer than a certain length), then copying will
mean allocating a new string, copying the data, then deleting
the old one. (It's things like this that motivated the entire
movable business in the next version of the standard.)

The most frequent reason for reserve(), however, is to ensure
the validity of iterators.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 8 '08 #6

"Andrey Tarasevich" <an**************@hotmail.comwrote in message
news:gf**********@aioe.org...

Thanks for the clarification. I hadn't realized that the size of the object
and object.size() were unrelated.

Nov 8 '08 #7

"Andrey Tarasevich" <an**************@hotmail.comwrote in message
news:gf**********@aioe.org...
Can you illustrate what you are talking about with an example? Normally
the return value is an iterator of the same kind as the one (the ones)
that was passed in. If you pass 'char*'s, you get back a 'char*'. No need
to convert anything.
char* foo(char* a, char* b, char* c, char* d)
{
iterator i = search(a,b,c,d);

return ???
}

If I understand you correctly, I can say:

return i;

Nov 8 '08 #8
barcaroller wrote:
"Andrey Tarasevich" <an**************@hotmail.comwrote in message
news:gf**********@aioe.org...
>Can you illustrate what you are talking about with an example? Normally
the return value is an iterator of the same kind as the one (the ones)
that was passed in. If you pass 'char*'s, you get back a 'char*'. No need
to convert anything.

char* foo(char* a, char* b, char* c, char* d)
{
iterator i = search(a,b,c,d);

return ???
}

If I understand you correctly, I can say:

return i;
That depends on what type 'iterator' is in your code.

Schobi
Nov 9 '08 #9

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:gf**********@news.datemas.de...
barcaroller wrote:
>1. If I pass pointers (char*) as iterators to an STL algorithm and the
return value is an iterator, can I convert that iterator to a pointer?
If yes, how?

For most of the standard algorithms, the return value type is the same as
the one you pass. It's already a pointer, no need to convert. Test it,
you'll see.
Just an added note, the way to convert an iterator to a pointer is to take
the address of the dereferenced iterator. I.E.

&(*it) would be a pointer to the item if it is an iterator or a pointer.
>2. What is the internal representation of vector<string>?

Implementation-defined.
Will the vector
contain the string objects or will it contain pointers/references to the
string objects? The reason I ask is that it is not clear to me how the
v.reserve() and &v[0] operations would work for a vector<string>.

The vector will contain a dynamic array of 'string' objects. The
'reserve' operation allocates the array capable of containing future
'string' objects (which will be constructed using 'placement new', most
likely). Taking the address of v[0] (which returns the reference to the
very first object in the inner array) will give you the starting address
of the inner array. What's there to [not] understand? Now, you probably
need to understand how 'string' works, as well. Do you?
As an added note you can find your implementation of std::string on your
system. Try opening up your string. file and taking a look. It can get
pretty esoteric but if you can understand that code you will be well on your
way to understanding C++.

Nov 11 '08 #10

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

Similar topics

12
by: Gaurav | last post by:
Hello I have a program that basically inverts the contents of files except first line. It compiles fine but gives me core dump on running. If i comment temp.clear() it runs fine, but i need...
1
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an...
4
by: misirion | last post by:
Ciao, vorrei poter scrivere e leggere su files binari dei vettori di stringhe, ed avrei implementato questo codice: ifstream fin(conf.c_str(),ios::binary); char inc; fin.read( inc,...
2
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? //...
10
by: Shafik | last post by:
Hello, I am new to C++. I know the reason is probably template instantiation problems ... but what's the *real* reason I cannot declare a: vector<stringv = vector<string>(4); Thanks!...
5
by: Etrex | last post by:
Hello, This is my first attempt at a c++ program, and it is a long post, please bear with me. I'm trying to read in a text file containing a firewall log, make the information...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1",...
42
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.