C++ using std::string - why? [closed] - c++

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 4 years ago.
Improve this question
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 ?

By implementing operator*, you allow type size_t to be "multiplied" by type string. The reason multiplied is in quotes is because you implement yourself what "multiply" means. In this particular implementation, the string is just appended to itself n times.
So 5 * std::string("Hallo") will result in HalloHalloHalloHalloHallo

Related

How do I parse with respect to char array indices (C++) [closed]

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
How do I parse a char array such as:
"2019-11-01T17:00:10.000Z"
I want 17:00:10.000 and nothing else. How do I parse with respect to the char array indices?
The simplest and safest way to deal with strings is to use the standard std::string. The std::string has many class member functions that can help with this. One of them is substr that can be used to create a new std::string from a part of a string.
Example:
#include <iostream>
#include <stdexcept>
#include <string>
std::string get_time_str(const std::string& timestamp) {
if(timestamp.size() != 24)
throw std::runtime_error("bad timestamp: "+ timestamp);
return timestamp.substr(11, 12); // start at pos 11 and 12 chars forward
}
int main() {
const char* ts = "2019-11-01T17:00:10.000Z";
std::cout << get_time_str(ts) << '\n';
}
Output:
17:00:10.000

Array of functions returning bool [closed]

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
I have these boolean functions that I am writing individually and I was thinking to create an array and then use a loop to go over each of them. Below are individual functions that I would like to place in the array.
bool A(void);
bool E(void);
bool O(void);
bool P(void);
bool U(void);
bool I(void);
bool C(void);
bool L(void);
bool D(void);
Can I do this?
You can have an array of std::functions (which is a generalized function pointer). Sample program:
#include <array>
#include <functional>
#include <iostream>
typedef bool Func(void);
Func A,E,O,P,U,I,C,L,D;
int main()
{
std::array<std::function<Func>, 9> arr = { A,E,O,P,U,I,C,L,D };
for ( auto&& f: arr )
std::cout << f() << '\n';
}
You will need to provide bodies for all of those functions of course.
Prior to C++11 the code would have been:
Func *arr[] = { A,E,O,P,U,I,C,L,D };
for (size_t i = 0; i != sizeof arr / sizeof arr[0]; ++i)
std::cout << arr[i]() << '\n';
Using modern C++ code gives you more safety and flexibility, so it is to be preferred if you have a modern compiler available.

C++ convert *string to int [closed]

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 8 years ago.
Improve this question
Okay So i have this function
long valid(const char* str)
{
int temp = atoi(str)
return long;
}
`str = 9789070002046` //13 digit long
the function returns some random 10 digit number.I have also tried STOI stringstream
#shivam-gupta hi buddy,
You maybe have been quite new to C/C++ programming is that correct? Some people at the forum require you to make explain what your problem is in detail also with the right C++ syntax.
I know what you mean, and it had been a main problem with me in the beginning when I tried to learn C/C++. The function that you might be looking at is atol() not atoi(). From my point of view it is better to convert the string to double, then cast it to a long/integer variable.
The code I used to complete your snippet is this
#include <iostream>
long valid(const char* str);
int main()
{
long test;
const char *str = "9789070002046"; //13 digit long
test = valid(str);
std::cout << test << " sizeof test is " << sizeof(test) << std::endl;
return 0;
}
long valid(const char* str)
{
long temp = (long)atol(str);
return temp;
}
Enjoy coding C++! And remember, life is not too short to learn C++!

how can i pass from char[i] to a double? [closed]

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 8 years ago.
Improve this question
I have information stored in an array of char and I would like to pass them to a single double value.
As an example, I want to pass it from,
data[0]=3;
data[1]=.;
data[2]=1;
data[3]=4;
data[4]=1;
data[5]=5;
data[6]=1;
to
double data = 3.14151;
How can I do it?
Thanks!
You can use functions std::stringstream from sstream or strtod from cstdlib or stod (If you are using C++11) as per your need.
Quoting example from cplusplus.com
// stod example
#include <iostream> // std::cout
#include <string> // std::string, std::stod
int main ()
{
std::string orbits ("365.24 29.53");
std::string::size_type sz; // alias of size_t
double earth = std::stod (orbits,&sz);
double moon = std::stod (orbits.substr(sz));
std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.\n";
return 0;
}
I am assuming that your array actually contains characters and you just forgot the apostrophs. I used an easier, less error-prone way to initialize data, with the added benefit that my version actually does a zero-termination, which yours didn't (as Axel correctly pointed out).
char const * data = "3.1415";
The solution would be:
#include <cstdlib>
// ...
double number = std::strtod( data, NULL );
// ...
Check the strtod() documentation for behaviour in case of error, and how to use the second parameter to check if your conversion actually went as far as you expected.

C++ typeid.name() only returns char "c" [closed]

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 8 years ago.
Improve this question
I'm not entirely sure what is going on here. I'm guessing because my input is a string and I'm cycling through it one character at a time it is always returning as type char.
I pretty sure a string is actually char*. The only way I can think to fix this is to include and check what type of character it is, but I'd like to avoid doing that. Is there an alternative method using typeid.name() to figure out what the char is?
I'm using gcc compiler
voidQueue outQueue;
string temp = "32ad1f-31f()d";
int i = 0;
while(temp[i] != '\0')
{
outQueue.enqueue(temp[i]);
i++;
}
template<typename T>
void voidQueue::enqueue(T data)
{
T *dataAdded = new T;
*dataAdded = data;
string type(typeid(data).name());
cout<< type;
myQueue::enqueue((void *)dataAdded,type);
}
i want it recognize that char('9') is actually an int
You can use std::isdigit for this:
#include <cctype>
bool digit = std::isdigit(static_cast<unsigned char>(temp[i]);
In your example, T is char and gcc returns "c" for typeid(char).name(), as demonstrated by the following program:
#include <iostream>
#include <typeinfo>
int main() {
std::cout << typeid(char).name() << std::endl;
std::cout << typeid(short).name() << std::endl;
std::cout << typeid(int).name() << std::endl;
std::cout << typeid(long).name() << std::endl;
}
On my compiler, this prints out
c
s
i
l
Given that the name() strings are implementation-defined, this is compliant behaviour.