Char * to std::string
I have a function :
Char* getlowestnumber(char* str, int n)
{
......
}
I have to convert str from type char*
to std::string
to perform some string operations.
I tried a lot of things but I couldn't. Do help me out.
See also questions close to this topic
-
omp parallel for - loop execution stuck after 1/6 of iterations have been done (with 6 threads)
I'm having problems parallelizing my code. I'm using C++/CLI with windows forms in VS2017. I was used to just write my parallel loops with concurrency parallel_for but crt is not supoported with /clr, which forces me to use OpenMP. I am a beginner with omp and the research I made didn't give me a solution to my problem. My code runs completely fine without the "#pragma omp parallel for" line. I only want to parallelize the inner loop, because that's where the most computationally expensive part is. Inside the inner loop, I am using some variables from outside both loops, but I only read them, I do not change them. All the other variables are declared inside the inner loop, which should not cause any problems. My code runs untill some time and then just freezes, not computing anything (according to my task manager).
I played around with the loop ranges and found out, that when I change the parallel for loop range, the freeze occurs at a different iteration - I eventually figured it's always at 1/6 of the loop range (my CPU has 6 single-threaded cores). Only one core seems to be computing only 1/6 of the loop. I tried to run some other code with the same "#pragma omp parallel for" syntax to check if my compiler or project settings are right, and with the simple test code, I am really getting 100% utilization, roghly 6x speedup. Any ideas, what might be wrong?
for (int mimosloupeciter = 0; mimosloupeciter < iters_horizontal; mimosloupeciter++)//X cyklus { double beta = asin(abs((double)mimosloupeciter - floor((double)iters_horizontal / 2)) / R); #pragma omp parallel for for (int meridianiter = 0; meridianiter < iters_vertical; meridianiter++)//Y cyklus { //some expensive computation } }
-
Is there is a difference in time and space complexicity between equalization sign and initialisation?
For example i have the following code:
int value = 10; int firstMethod = value; int secondMethod (value);
What is the difference in time and space if I use second method? Is it gonna take longer? Is there any reason why people write code with first method?
-
AWS lambda C++ runtime in SAM
Does anyone have a step-by-step tutorial on using SAM to package an AWS lambda function using the C++ runtime so I can run it locally? C++ is not one of the languages supported using
sam init --runtime
and I cannot work out the steps needed to package the Hello World function from https://aws.amazon.com/blogs/compute/introducing-the-c-lambda-runtime/ -
Segmentation fault while Inserting std::string into std::vector<std::string>
I encountered some weird behavior of insert function for std::vector using iterators.
What I'm trying to do is basically input the strings from text file in the alphabetical order, but when I use std::vector::insert function with strings I get an Segmentation fault (core dumped) message. std::vector::push_back works without any problems.
In other words, performing operation like this works:
std::vector<int> intVec; intVec.push_back(1); std::vector<int>::iterator it = intVec.begin(); intVec.insert(it, 5);
And operation like this causes the Segmentation fault:
std::vector<std::string> strVec; std::vector<std::string>::iterator it = strVec.begin(); strVec.push_back("some string"); strVec.insert(it, "some other string");
Inserting using variables like std::sting var = "some string" does not work either, nor using c_str() function.
Does anyone possibly knows why that problem may occur? Thanks for any explanation.
P.S. I can solve sorting problem using different mechanisms, just want to figure out what's going on here.
-
Conversion operator causing compilation error
I have implemented a string-like class, called Str, and want to add a conversion to bool type, in order to use a Str object as a condition.
The Str class is quite simple and relies on another template class called Vec, which is similar to the STL vector class. Below I have copied the main parts of the definition
class Str { friend std::istream& operator>>(std::istream&, Str&); private: Vec<char> data; public: typedef Vec<char>::size_type size_type; typedef Vec<char>::iterator iterator; typedef Vec<char>::const_iterator const_iterator; iterator begin() { return data.begin(); } const_iterator begin() const { return data.begin(); } iterator end() { return data.end(); } const_iterator end() const { return data.end(); } //[...] char& operator[](size_type i) { return data[i]; } const char& operator[](size_type i) const { return data[i]; } Str& operator+=(const Str& s) { std::copy(s.data.begin(), s.data.end(), std::back_inserter(data)); return *this; } }; Str::operator bool() const { if (data.size() > 0) return true; return false; }
Everything works fine, except that now, if I try for example to do the following:
Str greeting = "Hello, " + name + "!";
then I get a compilation error, saying "'operator +': 2 overloads have similar conversions, could be 'Str operator +(const Str &,const Str &)' or 'built-in C++ operator+(const char [8], int)'.
I understand the reason for the error, but I am not sure how to fix it. How can I get around this?
Thanks.
EDIT: below I have added a minimal, complete and verifiable example:
#include <iterator> #include <vector> #include<iostream> using namespace std; class Str { private: std::vector<char> data; public: typedef std::vector<char>::size_type size_type; typedef std::vector<char>::iterator iterator; typedef std::vector<char>::const_iterator const_iterator; iterator begin() { return data.begin(); } const_iterator begin() const { return data.begin(); } iterator end() { return data.end(); } const_iterator end() const { return data.end(); } // default constructor must be defined explicitly, since non-default constructors are also defined Str() { } Str(const size_type n, const char c) : data(n, c) { } Str(const char* cp) { std::copy(cp, cp + std::strlen(cp), std::back_inserter(data)); } // Since this is a template, the iterators can be anything: pointers in an array of chars, std::vector<string> iterators, std::std::vectortor<string> iterators, std::list<string> iterators, etc. template <class In> Str(In b, In e) { std::copy(b, e, std::back_inserter(data)); } char& operator[](size_type i) { return data[i]; } const char& operator[](size_type i) const { return data[i]; } Str& operator+=(const Str& s) { std::copy(s.data.begin(), s.data.end(), std::back_inserter(data)); return *this; } size_type size() const { return data.size(); } // conversion operators operator std::vector<char>() const; operator bool() const; template <class In> void insert(iterator dest, In b, In e) { data.insert(dest, b, e); } }; Str operator+(const Str& s1, const Str& s2) { Str s = s1; s += s2; return s; } Str::operator std::vector<char>() const { std::vector<char> ret = data; return ret; } Str::operator bool() const { if (data.size() > 0) return true; return false; } int main() { Str name = "Joe"; Str greeting = "Hello, " + name + "!"; return 0; }
-
C++ using std::string - why?
I wanted to ask,
how does string::string operator function, I know that it is a standard constructor, for using strings, yet what does operator do? Does it allow me to use the multiplier operator at the end? Size_t represents the size of an object and string& is a pass by reference. How are these concepts making sense?
#include <iostream> #include <string> using namespace std::literals::string_literals; std::string operator*(std::size_t n, const std::string& s) { std::string ret; while (n--) ret += s; return ret; } int main() { std::cout << 5 * std::string("Hallo") << std::endl; std::cout << 5 * "Test"s << std::endl; }
What does std::string ret mean, can I use it because of std::string? Because std::string has been defined at the beginning ?