C++ strings vs vector<char> [duplicate] - c++

This question already has answers here:
What are differences between std::string and std::vector<char>?
(5 answers)
C++: char test[100] vs array<char, 100> vs string
(4 answers)
Closed 7 years ago.
It's known to everyone of us that we should prefer string class in C++ for all string applications due to the many special functions they perform & their ability to grow & reduce dynamically. What string is for characters, vector is for other data types & classes because it shows great performance.
However is there any situation where we would need to prefer vector<char> (which I see seldom) over string ?

I'd use vector<char> only if I explicitly intent to store an array of char values, which is not a string. E.g. if for some reason I'd collect all the characters used somewhere in a specific text, the result might be a vector<char>.
To be clear: it is all about expressing the intent.

To put it briefly: if you're storing text, then string, otherwise vector<char>.

Related

Is there a way to multiply a string and an integer in C++? [duplicate]

This question already has answers here:
Multiplying a string by an int in C++
(10 answers)
Closed 2 years ago.
In Python, there is a way to multiply a string, for example, "A" * 3 would return "AAA". However, this syntax doesn't work in C++, it just gives me an error, invalid operands of types 'const char [2]' and 'int' to binary 'operator*'. Is there a way to multiply a string like this in C++?
There is no such operator in C++.
But you can write a function that takes a string and a number as argument, and return the repeated string.
std::string also has a constructor that repeats given character a number of times. This may be useful since you used a string of one character as the example.
You can overload an operator
std::string operator*(const std::string& s, unsigned n)
{
... // your code here
}
Not really... Although you can right one yourself. C++ is a very different beast to Python. It's much more low level and It is not as forgiving.
C++ deals with "real" memory (as it were).
You can't really just say "A" * 3 because before the "A" simply used 1 byte of memory if you have 3 "A"'s you need 3 bytes. and since C++'s main benefit is in it's ability to allow the user to author It's memory use, C++ won't just create 3 extra bytes.
You can implement your own version of Python's "A" * 3 by using the overload operator (although you would have to use a class type like string) C++ provides But I wouldn't really recommend this.
I'd suggest just using a std::vector and append the character as many times as you would like.
Also have a think about what you are trying to really do. C++ is all about performance rather than usability. If you are trying to set multiple bytes to a certain value you can use std::memset

What is the best way to convert a string type to an int type as Industry standard? [duplicate]

This question already has answers here:
How to parse a string to an int in C++?
(17 answers)
Closed 6 years ago.
In C++, what is the best way to take input string and convert it to a number type. I was messing around with literal strings and was having a difficult time doing this, I am imagining that using a C String is easier? What is the best way for co-workers and fellow students to follow and make it easy and simple? Any thoughts?
What is the industry standard on this? Using STOI?
Look at the following example:
std::string numberString = "125";
int number = std::stoi(numberString ,nullptr);
It can convert an std::string to int

How to convert a double into a char* [duplicate]

This question already has answers here:
Converting double to char* in C++ with high performance
(9 answers)
Closed 7 years ago.
I am heavily struggling with a question which should be very easy: how do I do a simple type conversion (from double into char*) in basic C.
I have found quite some solutions, but they are all based on conversions from double to char[x], but I am working here with char*, not with char[]. (I don't know how long the resulting string will be).
On top of that, I really can't believe that another type (be it stringstream, std::strings, ...) are needed for something that simple.
I admit, I'm a complete newbie in basic C, but I have worked in other languages (Visual Basic, Delphi, Java, ...) and I just can't understand why I can't find a simple function like "to_char_pointer(double d)" to do this.
Does anybody have an idea?
You can use sprintf() as you have done to convert a double to a string, but I would actually recommend using _snprintf() instead simply because sprintf() has no regard for the fact that strings are fixed length devices in memory and will overflow if you don't watch it. _snprintf() allows you to specify the length of the out string, just be sure to specify the size as one less than the actual allocated memory block, because _snprintf() does not store the terminating null character if it has to cut the output short.
An example us using _snprintf() is:
void ToString(char * outStr, int length, double val)
{
_snprintf(outStr,length,"%f",val);
}

Format a String in C++ with the same convenience as String.format() in Java 5 / 6? [duplicate]

This question already has answers here:
How to construct a std::string with embedded values, i.e. "string interpolation"?
(8 answers)
Closed 2 years ago.
Is there a common function available to be able to do sprintf type String formatting without having to supply a fixed size buffer, that returns a string class instance?
I know about stringstream it doesn't do what I want, I don't want to hard code the position of the tokens in the output statement like it requires.
I want to be able to define a pattern like sprintf lets you, but without the C baggage and in a more idiomatic Object Oriented C++ manner.
Maybe some function that does what sprintf does using a stringstream and produces a string object? Something along the line of the convenience of what String.format() does in Java or the equivalent String formatting syntax in Python.
The Boost Format Library:
The <boost/format.hpp> format class provides printf-like formatting, in a type-safe manner which allows output of user-defined types.
If you don't use Boost.Format or Boost.Locale you can use my simple stringstream wrapper or wrap it even further:
fakeformat
example:
REQUIRE( ff::format("{2}ff{1}").with('a').also_with(7).now()=="7ffa" );
Ideone

How many characters does string class in c++ support? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Maximum length of a std::basic_string<_CharT> string
I would like to know how many characters does string class in c++ support.
thanks..
std::string s;
s.max_size();
That should tell you what that max size is.
according to http://www.idinews.com/string3.html
The std::string class supports varying length strings with no length limit.
I am lead to believe that the size of a std::string object is limited to size of an unsigned integer on the architecture of your system. I would assume this since the length of the string is stored as a size_t value.