This question already has answers here:
Convert a single character to a string?
(9 answers)
Closed 6 years ago.
I am practicing on LeetCode.
I have the following CPP code:
string s="abcd";
queue<string> q;
string temp;
temp=s[2];
queue.push(temp);
Error:
Line 5: conversion from
'__gnu_cxx::__alloc_traits >::value_type {aka
char}' to non-scalar type 'std::__cxx11::string {aka
std::__cxx11::basic_string}' requested
I am wondering why this happen? I can't really change thte structure of this because temp will be growing over time (eg., temp='a'+'b'+'c'; queue.push(temp));
Read your code again ,
I am sure that you wanted to write :
queue.push(temp);
like this :
q.push(temp);
Related
This question already has answers here:
escape R"()" in a raw string in C++
(2 answers)
Include )" in raw string literal without terminating said literal
(3 answers)
Closed 9 months ago.
I want to output a string like this: onclick="func()". So I wrote the following code:
std::string s = R"(
onclick="func()"
)";
But here two )" let the compiler confused.
Please forgive me if it's a silly question.
I googled but found nothing (I don't know which keyword I should use when I googled).
Simply add a unique string outside the ()
std::string s = R"anystring(
onclick="func()"
)anystring";
This question already has answers here:
Single quotes vs. double quotes in C or C++
(15 answers)
Closed 5 years ago.
Goal
At the end, I want to know why C++ doesn't support char letter = "C"; but does support char letter = 'C'; (notice that the quotation marks are different).
Code
I am using Repl.it as a code platform.
#include <iostream>
int main()
{
char letter = "C";
std::cout << letter;
}
Error message
main.cpp: In function 'int main()':
main.cpp:5:19: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
char letter = "C";
They are needed because 'C' and "C" represent completely different types - the first is an integer value, while the second is an array of two characters (the letter 'C' plus an implicit null-terminator). Both are useful, and you need some way of saying which one you want, which is what the different kinds of quotes do.
Single quotes are for single characters whereas double quotes are used to create string literals. They mean different things.
See a more thorough explanation.
This question already has answers here:
How to initialize a std::stringstream?
(2 answers)
Closed 6 years ago.
I want to reuse istringstream variable. It is easy to initial istringstream variable using construction function. But when I try to re-assign it to a new value using = or <<, I got error. I am using c++11. It seems no compile error in vs 2015 using =. But gcc 4.8.2 (devtools-2 on Centos 6.4 x86_64) get error.
Codes like below:
std::string line;
int simmulationtimes; double InitialIDensity;
// deploy configuration file
std::ifstream config(configfile);
if (!config.is_open())
{
return false;
}
std::getline(config, line);
std::istringstream sline(line);
std::string sInitialIDensity;
while(std::getline(sline, sInitialIDensity, '='));
InitialIDensity = std::stod(sInitialIDensity);
std::getline(config, line);
std::string ssimmulationtimes;
sline.str(""); sline.clear();
sline = std::istringstream(line);
while (std::getline(sline, ssimmulationtimes, '='));
simmulationtimes = std::stoi(ssimmulationtimes);
configuration file should be:
IDensity=0.5
times=5
Error:
error: no match for ‘operator=’ (operand types are ‘std::istringstream {aka std::basic_istringstream<char>}’ and ‘std::string {aka std::basic_string<char>}’)
sline = std::istringstream(line);
Solutions (like how-to-initialize-a-stdstringstream) on stackoverflow about reusing stringstream not work for me. Any ideas about reusing istringstream? Thanks for your consideration.
Use the str() method to set a new std::string into an existing std::istringstream.
This question already has answers here:
How to convert a std::string to const char* or char*
(11 answers)
Closed 7 years ago.
I'm using C++ (using CERN's ROOT framework) and I'm having a little problem with strings. I'm trying to label a histogram axis using a string defined by the user earlier in the code. Here are the relevant parts of the code:
string xlabel;
...
cout << "Enter x-axis label:" << endl;
getline(cin >> ws, xlabel);
...
hist->GetXaxis()->SetTitle(xlabel);
Where the last line is just syntax that ROOT uses (usually xlabel here would be in quotation marks and you can type in what you want the label to be, but I am trying to input the string defined earlier in the code.)
Anyway, when I compile this, I get the following error:
error: no viable conversion from 'string'
(aka 'basic_string<char>') to 'const char *'
hist->GetXaxis()->SetTitle(xlabel);
^~~~~~
I have tried re-defining xlabel as a const char * but it didn't like that either. Does anyone have any suggestions on how I could define this string?
Thanks in advance!
Do this:
hist->GetXaxis()->SetTitle(xlabel.c_str());
// ^^^^^^^^
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
This my code.
if(strcmp(s[0][i],s[rock][j])==0)
{
count[i]+=1;
rock++;
}
The strcmp function compares two strings. If you want to compare two char variables, just use == or !=.
It is obvious that objects s[0][i] and s[rock][j] have type char. That is they are not strings but two characters. You can compare them using operator ==
For example
if ( s[0][i] == s[rock][j] )
Also I would advice to substitute these two expression statements
count[i]+=1;
rock++;
for
count[i]++;
rock++;
or
++count[i];
++rock;
In this case the code looks more readable.
I will assume for this answer that s was declared as:
char s[100][100]; // or any arbitrary numeric limits
In this case, you cannot compare s[0][i] and s[rock][j] using strcmp, because they are characters, not character pointers.
Instead, you can just write:
if(s[0][i] == s[rock][j])
char is primitive datatype - you can compare chars directly:
...
if (s[0][i] == s[rock][j])
{
...
strcmp function expects strings, i.e. char *, hence the error message. You could compare whole strings in s variable:
...
if (strcmp(s[0], s[rock]) == 0)
{
...
For comparing to char don't use string compare function! The prototype of strcmp function is-
int strcmp(const char *s1, const char *s2); // where s1 and s2 are strings
So for comparing to characters just use == or !=
if(s[0][i]==s[rock][j])
{
count[i]++;
rock++;
}