Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
The recommendation for appending one vector to another (according to Concatenating two std::vectors) is to use:
vector1.insert( vector1.end(), vector2.begin(), vector2.end() );
Why does vector not have a method along the lines of:
vector1.append(vector2);
The only reason that comes to mind is that it may be unclear exactly what append does.
That's a pretty good reason. In fact, as an apt example, I think you've got the meaning of "append" wrong. I'd expect it to be equivalent to push_back (which exists), whereas you're looking for something more like "concatenate".
Why isn't there a concatenate function? Well, there could have been. But the standard doesn't like to give you things you don't really need, and (as you've shown) this operation is pretty easy to implement yourself.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am looking for a function to compare the two strings. A functional similar to strcmp in CString with the difference that takes two strands in the input.
You can use std::string::compare (it returns 0 if values are the same). Also be aware that in fact you can use strcmp in c++, but if you want modern c++ version i would go with std::string::compare.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Checking membership is one of the most frequently used operations in any programming languages, but C++ insists that programmers use the verbose container.find(value) != container.end() instead of the readable container.contains(value). What is the rationale behind not adding a syntactic sugar for this operation?
This is not true for all containers. For example; std::set has a .count() member function, that is essentially the same as contains, in that it will return 0 if the container does not contain an element and >0 if it does. There's also std::any_of which works for all containers.
There is a method to check if a container contains an object: std::any_of(). This is also more flexible since it takes a predicate to determine if the object is in the container.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've tried searching for the answer, but nothing mentions stringstream specifically. I would guess that it would always work and you can always go back as far as the beginning of the underlying string.
Am I right?
How likely is istream::ungetc() to work with a stringbuf (as used in stringstream)?
Well, never.
There's no such thing like istream::ungetc() defined from the standard.
You can use either
int std::ungetc( int ch, std::FILE *stream )
or
std::basic_istream& std::basic_istream::unget()
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
For years I have used stringname[0] to obtain the first char of a string without even thinking about it. However, I recently came to wonder if brute access to the array is really a good practice. This may seem a trivial question, but it is not because it looks deeply linked to memory/access management of STL containers.
I can think of stringname.at(0) (not very convincing) but there are probably better alternatives with an iterator. Most importantly, the ideal method would not cause an error if the string is empty.
Any widely accepted good practice for this ?
If s is an empty string, s[0] returns '\0' whereas s.at(0) throws std::out_of_range.
That difference in behavior is far more significant than any difference in performance.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How do you store contents in a vector and then sort them without creating a class?
Doing a class project and it requires to not use a class. The books are not helping.
This page has a good example, assuming you can put all of the elements in the vector at once. If you need to put them in one at a time, use std::vector::push_back.
Something like std::vector<std::string> v; v.push_back("hello"); std::sort(v.begin(), v.end());. Get better books.