string's size and capacity question

P

pass86

#include <iostream>
#include <string>
int main()
{
using namespace std;
string empty;
string small = "bit";
string larger = "Elephants are a girl's best firend";
cout << "Size:\n";
cout << "\tempty: " << empty.size() << endl;
cout << "\tsmall: " << small.size() << endl;
cout << "\tlarger: " << larger.size() << endl;

cout << "Capactites:\n";
cout << "\tempty: " << empty.capacity() << endl;
cout << "\tsmall: " << small.capacity() << endl;
cout << "\tlarger: " << larger.capacity() << endl;

empty.reserve(50);
cout << "Capacity after emtpy.reserve(50): ";
cout << empty.capacity() << endl;

return 0;
}

I get result:
Size:
empty: 0
small: 3
larger: 34
Capactites:
empty: 0
small: 3
larger: 34
Capacity after emtpy.reserve(50): 50

It is different from 《C++ Primer plus 5th》16.4str2.cpp.
 
J

Jim Langston

#include <iostream>
#include <string>
int main()
{
using namespace std;
string empty;
string small = "bit";
string larger = "Elephants are a girl's best firend";
cout << "Size:\n";
cout << "\tempty: " << empty.size() << endl;
cout << "\tsmall: " << small.size() << endl;
cout << "\tlarger: " << larger.size() << endl;

cout << "Capactites:\n";
cout << "\tempty: " << empty.capacity() << endl;
cout << "\tsmall: " << small.capacity() << endl;
cout << "\tlarger: " << larger.capacity() << endl;

empty.reserve(50);
cout << "Capacity after emtpy.reserve(50): ";
cout << empty.capacity() << endl;

return 0;
}

I get result:
Size:
empty: 0
small: 3
larger: 34
Capactites:
empty: 0
small: 3
larger: 34
Capacity after emtpy.reserve(50): 50

It is different from ?C++ Primer plus 5th?16.4str2.cpp.

======================

And on my system I get:

Size:
empty: 0
small: 3
larger: 34
Capactites:
empty: 15
small: 15
larger: 47
Capacity after emtpy.reserve(50): 63

It is system dependant.
 
W

wxghust

#include <iostream>
#include <string>
int main()
{
using namespace std;
string empty;
string small = "bit";
string larger = "Elephants are a girl's best firend";
cout << "Size:\n";
cout << "\tempty: " << empty.size() << endl;
cout << "\tsmall: " << small.size() << endl;
cout << "\tlarger: " << larger.size() << endl;

cout << "Capactites:\n";
cout << "\tempty: " << empty.capacity() << endl;
cout << "\tsmall: " << small.capacity() << endl;
cout << "\tlarger: " << larger.capacity() << endl;

empty.reserve(50);
cout << "Capacity after emtpy.reserve(50): ";
cout << empty.capacity() << endl;

return 0;

}

I get result:
Size:
empty: 0
small: 3
larger: 34
Capactites:
empty: 0
small: 3
larger: 34
Capacity after emtpy.reserve(50): 50

It is different from ¡¶C++ Primer plus 5th¡·16.4str2.cpp.

yes, i ran this program in DEV C++, and get the same result,
i change string to vecter, get the same result too.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char *argv[])
{
std::vector<char> empty;
std::vector<char> small;
std::string foo = "bit";
small.assign(foo.begin(), foo.end());
std::string foo2 = "Elephants are a girl's best firend";
std::vector<char> larger(foo2.begin(), foo2.end());

cout << "Size:\n";
cout << "\tempty: " << empty.size() << endl;
cout << "\tsmall: " << small.size() << endl;
cout << "\tlarger: " << larger.size() << endl;

cout << "Capactites:\n";
cout << "\tempty: " << empty.capacity() << endl;
cout << "\tsmall: " << small.capacity() << endl;
cout << "\tlarger: " << larger.capacity() << endl;


empty.reserve(50);
cout << "Capacity after emtpy.reserve(50): ";
cout << empty.capacity() << endl;

system("PAUSE");
return 1;
}

i think maybe there are some problems in primer.
 
W

wxghust

#include <iostream>
#include <string>
int main()
{
using namespace std;
string empty;
string small = "bit";
string larger = "Elephants are a girl's best firend";
cout << "Size:\n";
cout << "\tempty: " << empty.size() << endl;
cout << "\tsmall: " << small.size() << endl;
cout << "\tlarger: " << larger.size() << endl;

cout << "Capactites:\n";
cout << "\tempty: " << empty.capacity() << endl;
cout << "\tsmall: " << small.capacity() << endl;
cout << "\tlarger: " << larger.capacity() << endl;

empty.reserve(50);
cout << "Capacity after emtpy.reserve(50): ";
cout << empty.capacity() << endl;

return 0;

}

I get result:
Size:
empty: 0
small: 3
larger: 34
Capactites:
empty: 0
small: 3
larger: 34
Capacity after emtpy.reserve(50): 50

It is different from ?C++ Primer plus 5th?16.4str2.cpp.

======================

And on my system I get:

Size:
empty: 0
small: 3
larger: 34
Capactites:
empty: 15
small: 15
larger: 47
Capacity after emtpy.reserve(50): 63

It is system dependant.

What compiler do you use?
 
J

Jim Langston

#include <iostream>
#include <string>
int main()
{
using namespace std;
string empty;
string small = "bit";
string larger = "Elephants are a girl's best firend";
cout << "Size:\n";
cout << "\tempty: " << empty.size() << endl;
cout << "\tsmall: " << small.size() << endl;
cout << "\tlarger: " << larger.size() << endl;

cout << "Capactites:\n";
cout << "\tempty: " << empty.capacity() << endl;
cout << "\tsmall: " << small.capacity() << endl;
cout << "\tlarger: " << larger.capacity() << endl;

empty.reserve(50);
cout << "Capacity after emtpy.reserve(50): ";
cout << empty.capacity() << endl;

return 0;

}

I get result:
Size:
empty: 0
small: 3
larger: 34
Capactites:
empty: 0
small: 3
larger: 34
Capacity after emtpy.reserve(50): 50

It is different from ?C++ Primer plus 5th?16.4str2.cpp.

======================

And on my system I get:

Size:
empty: 0
small: 3
larger: 34
Capactites:
empty: 15
small: 15
larger: 47
Capacity after emtpy.reserve(50): 63

It is system dependant.

What compiler do you use?

==============

Microsoft Visual C++ .net 2003
 
J

Jim Langston

#include <iostream>
#include <string>
int main()
{
using namespace std;
string empty;
string small = "bit";
string larger = "Elephants are a girl's best firend";
cout << "Size:\n";
cout << "\tempty: " << empty.size() << endl;
cout << "\tsmall: " << small.size() << endl;
cout << "\tlarger: " << larger.size() << endl;

cout << "Capactites:\n";
cout << "\tempty: " << empty.capacity() << endl;
cout << "\tsmall: " << small.capacity() << endl;
cout << "\tlarger: " << larger.capacity() << endl;

empty.reserve(50);
cout << "Capacity after emtpy.reserve(50): ";
cout << empty.capacity() << endl;

return 0;

}

I get result:
Size:
empty: 0
small: 3
larger: 34
Capactites:
empty: 0
small: 3
larger: 34
Capacity after emtpy.reserve(50): 50

It is different from ¡¶C++ Primer plus 5th¡·16.4str2.cpp.

yes, i ran this program in DEV C++, and get the same result,
i change string to vecter, get the same result too.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char *argv[])
{
std::vector<char> empty;
std::vector<char> small;
std::string foo = "bit";
small.assign(foo.begin(), foo.end());
std::string foo2 = "Elephants are a girl's best firend";
std::vector<char> larger(foo2.begin(), foo2.end());

cout << "Size:\n";
cout << "\tempty: " << empty.size() << endl;
cout << "\tsmall: " << small.size() << endl;
cout << "\tlarger: " << larger.size() << endl;

cout << "Capactites:\n";
cout << "\tempty: " << empty.capacity() << endl;
cout << "\tsmall: " << small.capacity() << endl;
cout << "\tlarger: " << larger.capacity() << endl;


empty.reserve(50);
cout << "Capacity after emtpy.reserve(50): ";
cout << empty.capacity() << endl;

system("PAUSE");
return 1;
}

i think maybe there are some problems in primer.

=============

The primer is showing the results from his system. System being OS and
compiler. Each compiler can do it different as long as they follow the
standard. Getting extra memory is okay, as long as it gets at least what
you requested, which yours did.
 
G

Guest

yes, i ran this program in DEV C++, and get the same result,
i change string to vecter, get the same result too.

i think maybe there are some problems in primer.

Nope, everything is working just fine, for you, for the OP, for Jim
Langston, and for the author of the book. reserve() is only guaranteed
to increase the capacity to at least the amount specified, it might
increase it more. In other words after doing

c.reserve(N);

the following must be true:

c.capacity() >= N
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top