Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
How can I append a string to an element of an string vector?
I consistently get segmentation fault.
Appending str to the ith element of vec:
vec[i] += str;
Assuming vec is something like a std::vector<std::string> and the ith element exists.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I've seen a code and there was string* name. Isn't it wrong? I mean string name is already creating a vector of characters, what would there be string* ? Thank you!
It would be something like
string *xyz = new string (...)
which is a string pointer.
Can you please post the part of a code here of what you saw/made.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Is there a way to terminate a C++ string at any arbitary location. This is very easy in C as we can just insert a null character wherever we want. But how can the same be achieved in C++ String.
For example, Let's consider the following example,
string str = "This is Stack OverflowXXXX";
Now I want to terminate this string so that I would get "This is Stack Overflow".
Yep! Use string::erase:
str.erase(k);
will erase all characters from position k forward. There's no way to "undo" this to get those characters back, though.
Hope this helps!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I have an array of type double. How do I get the 10 lowest values?
double values[1000];
This is what I've come up before:
double similar[num_img];
copy(begin(values), end(values), begin(similar)); //copy values to another variable
int elements = sizeof(similar) / sizeof(similar[0]);
sort(similar, similar + elements);
So that I could get the 10 values. But what I'm actually after is the indices.. So sorting it would not help, I guess.
Sort the array and grab the first 10 elements (values[0] through values[9]).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I was wondering what the following code does:
for (auto x:m) std::cout << x << " ";
I already know that auto is a way to leave it to the compiler to decide the type of the variable but I don't know what :m does.
It is a C++11 range-based for loop syntax described here: http://www.cprogramming.com/c++11/c++11-ranged-for-loop.html
Here m should be a container, like std::vector. The code will iterate the container and put every element (accessed as x inside the loop) into the std::cout stream. Elements will be separated by space.
m is any type that follows the ranged concept (i.e. Container concept).
The loop iterates over all elements of m where x represents the currently iterated value.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I mean the way that is done in editors with insert key on.
So having string like:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The effect will be:
~~~~~~~~~~Hello!~~~~~~~~~~~~~~~~~~
that is without changing length of the string.
Overwriting a portion of a string is done with one of the several overloads of std::string's replace member function, for example:
string str = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
string rep = "Hello!";
cout << str.replace(5, rep.size(), rep) << endl;
You can play with this example at ideone [link].
The simplest solution is probably to use std::copy, with the
appropriate iterators:
std::copy( newText.begin(), newText.end(), str.begin() + n );
Just be sure that the target string is big enough.