I have this function in C++11:
bool ccc(const string cc) {
vector<string> digits;
int aux;
for(int n = 0; n < cc.length(); ++n) {
digits.push_back(to_string(cc[n])); }
for(int s = 1; s < digits.size(); s += 2) {
aux = stoi(digits[s]);
aux *= 2;
digits[s] = to_string(aux);
aux = 0;
for(int f = 0; f < digits[s].length(); ++f) {
aux += stoi(digits[s][f]); }
digits[s] = to_string(aux);
aux = 0; }
for(int b = 0; b < digits.size(); ++b) {
aux += stoi(digits[b]); }
aux *= 9;
aux %= 10;
return (aux == 0); }
And I get this error when compiling with g++ with the -std=c++11 flag:
crecarche.cpp: In function ‘bool ccc(std::string)’:
crecarche.cpp:18:12: error: no matching function for call to ‘stoi(__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type&)’
18 | aux += stoi(digits[s][f]); }
| ~~~~^~~~~~~~~~~~~~
But I used the stoi function after and I did not get any error with that line.
Why is the compiler throwing me this error and how can I fix it?
The error message is telling you that the argument you pass to stoi is of the type
__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type&
which is a fancy way of saying char&. This happens because digits[s] is already of type string&, and subscribing it further gives you a char&.
It's not clear to me what you are trying to accomplish. Maybe you need to remove the extra subscript, or use digits[s][f] - '0' to compute the digit value. C++ requires that the decimal digits are represented by subsequent code points, so this works even in theoretical implementations which are not based on the ISO 646 subset of Unicode.
Related
Fairly new to coding. Trying some of the easy projects at LeetCode, and failing... Ha! I am trying to take an integer and convert it to a string so I can reverse it, then re-convert the reversed string back into a integer.
This code is throwing the "terminate after throwing and instance of 'std::invalid argument' what(): stoi" error. I've spent an hour searching google and other questions here on SO, but can't figure out why it's not working.
bool isPalindrome(int x) {
std::string backwards ="";
std::string NumString = std::to_string(x);
for (int i = NumString.size(); i >= 0 ; i--) {
backwards += NumString[i];
}
int check = std::stoi(backwards);
if (check == x) {
return true;
}
else {
return false;
}
}
EDIT: I think I figured it out. It was adding the null character to the end of the string upon first conversion, then adding it to the beginning of the string when I reversed it. Spaces can't be converted to integers.
So... I changed this line and it works:
for (int i = NumString.size() - 1; i >= 0 ; i--)
you can also reverse number without using string.
bool isPalindrome(int x) {
long long rev = 0;
int cur = x;
while( cur > 0) {
rev *= 10;
rev += cur % 10;
cur /=10;
}
return rev == x;
}
Its simpler than your answer that you edited in. YOu have
for (int i = NumString.size(); i >= 0 ; i--) {
backwards += NumString[i];
}
Imagine that Numstring has length 3 (no matter what spaces, digits,....)
So now you are efectively doing
for (int i = 3; i >= 0 ; i--) {
backwards += NumString[i];
}
So first loop goes
backwards += NumString[3];
well the indexes of things in an array of length 3 in c++ are 0,1,2. YOu are going one off the end
This is why you see loops doing
for(int i = 0; i < len; i++){}
Note the i < len not i <= len
I have a c++ function for an RRC filter. The code. compiles and runs smoothly, but it gives a NaN output for a specified complex function.
The code in question is:
std::complex<float> rrc_output(const std::vector<std::complex<float>>& ht,
const std::deque<std::complex<float>>& rrc_sample_buffer){
std::complex<float> yt = 0.;
for (int n = 0; n < ht.size(); n++)
yt += ht[n]*rrc_sample_buffer[n];
return yt;
}
For more. clarity, the rrc _sample_buffer takes in 2-tuple inputs, (x,y) from a text file, and the ht is defined by the following formulae with conditions attached:
for (int i = 1; i< ht.size()+1; i++){
if (t == 0){
ht[i] = Rs*(1+(beta*((4/pi)-1)));
}
else if ( t == (Ts/(4*beta)) || t == -(Ts/(4*beta))){
ht[i] = (beta/Ts*sqrt(2))*((1+(2/pi))*sin(pi/(4*beta))+(1-(2/pi))*cos(pi/(4*beta)));
}
else{
ht[i] = (Rs*(sin(pi*t*Rs*(1-beta))+(4*beta*t*Rs*cos(pi*t*Rs*(1-beta)))))/(pi*t*Rs*(1-pow((4*beta*t*Rs),2)));
}
where the length of ht and rrc_sample_buffer are:
vector<complex<float>> ht(sps, 0);
std::deque<complex<float>> rrc_sample_buffer(ht.size(), 0.);
Any ideas on how I can get this fixed?
Thank you.
i am trying to write a program that searches through a movie script using two different string searching algorithms. However the Warning C26451: Arithmetic overflow using operator '+' on a 4 byte value then casting the result to 8 byte value keeps on coming up in the calculate hash part of the rabin karp, is there anyway to fix this? Any help would be greatly appreciated.
#define d 256
Position rabinkarp(const string& pat, const string& text) {
int M = pat.size();
int N = text.size();
int i, j;
int p = 0; // hash value for pattern
int t = 0; // hash value for txt
int h = 1;
int q = 101;
// The value of h would be "pow(d, M-1)%q"
for (i = 0; i < M - 1; i++)
h = (h * d) % q;
// Calculate the hash value of pattern and first
// window of text
for (i = 0; i < M; i++)
{
p = (d * p + pat[i]) % q;
t = (d * t + text[i]) % q;
}
// Slide the pattern over text one by one
for (i = 0; i <= N - M; i++)
{
// Check the hash values of current window of text
// and pattern. If the hash values match then only
// check for characters on by one
if (p == t)
{
/* Check for characters one by one */
for (j = 0; j < M; j++)
{
if (text[i + j] != pat[j])
break;
}
// if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1]
if (j == M)
return i;
}
// Calculate hash value for next window of text: Remove
// leading digit, add trailing digit
if (i < N - M)
{
t = (d * (t - text[i] * h) + text[i + M]) % q;// <---- warning is here
[i + M
// We might get negative value of t, converting it
// to positive
if (t < 0)
t = (t + q);
}
}
return -1;
}
context for the error
You're adding two int which is 4 bytes in your case, whereas std::string::size_type is probably 8 bytes in your case. Said conversion happens when you do:
text[i + M]
Which is a call to std::string::operator[] taking a std::string::size_type as parameter.
Use std::string::size_type, which is usually the same as size_t.
gcc does not give any warning for that, even with -Wall -Wextra -pedantic, so I guess you activated really every warning you can, or something similar
I would like to ask a question that make me confuse. I am trying so scan string and covert that into real number. Using that number to calculate value. Here is my code:
string input_file_name1 = "shen_test_38_30_60__78_26_38_b_100_ch1-533.0-mhz-8000.0-ksps-2016-06-20-17.24.19-utc.dat";
string input_file_name2 = "shen_test_38_30_60__78_26_38_b_100_ch2-533.0-mhz-8000.0-ksps-2016-06-20-17.24.19-utc.dat";
std::ifstream input1(input_file_name1.c_str() , std::ios::binary | std::ios::in);
std::ifstream input2(input_file_name2.c_str() , std::ios::binary | std::ios::in);
split(input_file_name1, '-', v);
for(unsigned i=1; i < v.size(); i++)
{
if(v[i] == "mhz"){
f_0 = atoi(v[i-1].c_str())*1e6;
}
if(v[i] == "ksps"){
f_s = atoi(v[i-1].c_str()) * 1e3;// f_s = 8e6;
}
}
double nblocks; //f_s = 8e6;
nblocks = floor(10 / (262144 / f_s));
when I compile I got this message:
" warning: ‘f_s’ may be used uninitialized in this function [-Wmaybe-uninitialized] nblocks = (10 / (nsamps / f_s));"
Do you have any ideal to help me fix this problem?
Thank you so much.
That means that if v[i] is neither "mhz" nor "ksps", then the code that assigns something to f_s is never executed and therefore leaving f_s uninitialized.
You can prevent this warning for example like this:
for(unsigned i=1; i < v.size(); i++)
{
if(v[i] == "mhz"){
f_0 = atoi(v[i-1].c_str())*1e6;
}
else if(v[i] == "ksps"){
f_s = atoi(v[i-1].c_str()) * 1e3;// f_s = 8e6;
}
else
{
// v[i] is none of the expected values
f_s = -1;
... take more action
}
}
The issue here is that if(v[i] == "ksps") may never be true. If it is not then f_s never gets a value set to it. What you can do is default initialize f_s with some value. Then you at least know the variable has some known state.
Do note that if you have not intialized f_0 you will have the same issue with it.
I have started doing some stuff with dynamic allocation in C++ but I had some problems. Here's the code:
nrMare(char cifS[], char* startPos = new char())
{
n = 0;
int i;
cif = startPos;
printf("%p %i\n", cif, (cif - (char*)NULL) % 8);
for(i = strlen(cifS) - 1; i >= 0; i--)
{
cif--;
n++;
cif = new(cif) char(cifS[i] - '0');
}
}
~nrMare()
{
int i;
for(i = 0; i < n; i++)
{
delete(cif);
cif++;
}
n = 0;
cif = 0;
}
nrMare is a class (it comes from bigNumber in Romanian :D) which is supposed to be able to contain the digits of a big number.
The problem is that the destructor (~nrMare) gives a weird error, when I make a variable nrMare something() on my computer, but it works for 116 digits long ones.
Do you have any suggestion or explainations?
EDIT: cif is a (char*) type
EDIT #2: n is the length of the number. I use the char pointer this way because I want to be able to add (like n++; cif--; cif = new(cif) char(number_to_add); -> this would add number_to_add in the left side of cif) and draw elements from both sides.
EDIT #3: this is gonna be a long one... Sorry for being such a bad explainer and thanks for your patience.here are some operators:
void operator-=(nrMare nr2)
{
int i;
for(i = 1; i <= n && i <= nr2.n; i++)
cif[n - i] -= nr2[nr2.n - i];
for(i = n - 1; i >= 0; i--)
{
if(cif[i] < 0)
{
cif[i] += 10;
cif[i - 1]--;
}
}
while(cif[0] == 0)
{
cif++;
n--;
//delete(cif - 1);
}
}
int operator/=(int nr)
{
int i;
for(i = 0; i < n - 1; i++)
{
cif[i + 1] += (cif[i] % nr) * 10;
cif[i] = cif[i] / nr;
}
i = cif[n - 1] % nr;
cif[n - 1] /= nr;
while(cif[0] == 0)
{
cif++;
n--;
//delete(cif - 1);
}
return i; // the return value is this big number % nr
}
void operator*=(int cifTimes)
{
int i;
for(i = 0; i < n; i++)
{
cif[i] *= cifTimes;
}
for(i = n - 1; i >= 0; i--)
{
if(cif[i] > 9)
{
if(i != 0)
{
cif[i - 1]++;
cif[i] %= 10;
}
else
{
n++;
cif[0] %= 10;
cif--;
cif = new(cif) char(cif[0] = 1);
}
}
}
}
EDIT #4: n = length of the number = number of digits = number of bytes. Weird error means it just crashes. I don't know how to find more about it. MinGW compiler asks Visual Studio (Visual C++) to debug it because it has some problems. This is for a problem, and somewhere (in the evaluator) it says "Killed by signal 6(SIGABRT)", if this helps.
EDIT #...: #Branko Dimitrijevic: I don't wanna be lazy... I want my own... I had this problem in more attempts to make something running. If I take out the destructor, it works just fine, just I guess then it would be a memory leak that way... I really want to find out why would this occur... and only for specific sizes and, i.e. it doesn't crash on the first "delete", but on the 11'th in my case, that's why it's weird .
The delete can only work correctly on an address that is at the beginning of a dynamically-allocated block.
The cif will fail one or both of these conditions, leading to undefined behavior when the destructor calls delete, for following reasons:
You assign startPos to cif and then modify it in a very strange way before calling the placement new. So even if startPos is a properly allocated block of dynamic memory, the cif no longer points to the starting address of it.
If the caller passes an address of a stack-based variable to startPos, then you no longer deal with dynamic memory at all.
Not to mention that you call new and delete in a loop - what's up with that? There is also a fair chance for bombarding the memory unless you craft your input parameters in a very specific way. This whole block of code looks suspicious, what exactly are you trying to do?