我正在编写一个“猪拉丁”程序;读取用户的输入(名字和姓氏),使输入变为小写并根据名称中的内容更改名称。如果第一个字母(名字和姓氏)都是元音,我们应该在其末尾添加“ way”。
如果第一个字母是辅音,我们将把第一个字母移到字符串的末尾,并在其末尾添加“ ay”。
尝试在字符串末尾添加文本时,我的代码给我错误。它说不能将字符串转换为字符,我不确定这意味着什么。它还说我不能将输出操作数<<用于字符串,即使我以前使用过它也是如此。
错误出现在“ strcpy”和我输出名称的最终代码中。
37:错误:无法转换
'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >'到'char*'的参数'1'来'char* strcpy(char*, const char*)'47:错误:无法转换
'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >'到'char*'的参数'1'来'char* strcpy(char*, const char*)'54:错误:不对应的
'operator<<'在'std::cout << first'
我只需要一些帮助来修复错误并查看我哪里出错了。包括完整的代码。
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
using namespace std;
int main()
{
int q, s;
char shea[] = "way";
char gavin_stop_looking_at_ponies[] = "ay";
vector …Run Code Online (Sandbox Code Playgroud) 我用c ++构建了一个简单的计算器,它使用了字符,所以(+, - *,/)将作为数学运算符工作,但当用户输入'='时它不起作用.
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
using namespace std;
#define PI 3.14159265359
#define NEWLINE '\n'
void printf(string string)
{
cout << string;
}
int main ()
{
char operation;
double a,b,c, value;
double answer = 0;
bool more = true;
cout << "Welcome to My Calculator\n\nInput a value: ";
cin >> answer;
operations:
cin >> operation;
if (operation != '=') {
if (operation == '+') {
cin >> value;
cout << "\n" << answer …Run Code Online (Sandbox Code Playgroud) 在一些std库template的参数中,需要定义他/她自己的函数比较器less(a, b),more(a, b)那么std::some_template<T, *, myComparator()>,为什么呢?
在可能的情况下使用std::string或单身更好char吗?
在我班上我想存储某些字符.我有CsvReader
课,我想存储columnDelimiter角色.我想知道,将它作为char或更好地使用它会更好std::string吗?在使用方面我觉得std::string好多了,但我想也许会有重大的性能差异?
如果我理解正确,那么对象'A'定义如下:
typedef struct {
int n;
float *p;
} myStruct;
myStruct A;
Run Code Online (Sandbox Code Playgroud)
是一个聚合,其内存布局与对象'B'完全相同,定义如下:
template <typename T> class myTemplateClass
{
public:
int n;
T* p;
};
myTemplateClass<float> B;
Run Code Online (Sandbox Code Playgroud)
那么,是否有更优雅的分配方式
A = B;
Run Code Online (Sandbox Code Playgroud)
而不是写
A = *(reinterpret_cast< myStruct *>(&B));
Run Code Online (Sandbox Code Playgroud)
每次?
我的理由是我必须调用一个库函数,该函数使用"myStruct"形式的参数公开一个接口,从代码中以myTemplateClass的形式保存我的数据是非常自然的.
我想为一个类型创建多个别名,我真正需要的是如下所示,
using MIN = MAX = AVG = nano_t;
Run Code Online (Sandbox Code Playgroud)
(这似乎更优雅,更少打字,而且还有两个以上的情况我必须做这种任务),而不是这样做:
using....
using...
using... every time
Run Code Online (Sandbox Code Playgroud)
但单行分配对我想要的编译器没有意义.有没有其他方法可以做到这一点?
我正在使用 C++11 来做一些线程程序。
现在我遇到这样的情况:
我有一个std::set<std::future<std::string>> results存储线程的一些结果,当然所有这些线程都会返回一个字符串。
但是,当我尝试获取字符串时,出现以下错误:
将 xxx 作为 xxx 的“this”参数传递会丢弃限定符
根据此链接,我认为这是因为我试图调用属于 元素的非常量函数set。换句话说, the 的元素set是std::future<std::string>并且std::future<std::string>::get()是非常量。这就是为什么我收到这样的错误。
如果我是对的,这是否意味着我永远不能声明 astd::set<std::future>因为它get总是不可用?
这是我的代码:
set<future<string>> results;
results.insert(...); // insert some future
for(auto it = results.begin(); it != results.end();)
{
if (it->wait_for(std::chrono::seconds(0)) == std::future_status::ready)
{
string tmp = it->get(); // ERROR!!!
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码来测试复制ctor并移动std::string类的ctor,结果让我感到惊讶,移动ctor 比复制ctor慢约1.4倍.
据我所知,移动构造不需要分配内存,因为在这种std::string情况下,移动构造对象中可能有一个内部指针直接设置为移动对象的内部指针,它应该比为缓冲区分配内存更快然后在复制构造时复制对象中的内容.
这是代码:
#include <string>
#include <iostream>
void CopyContruct(const std::string &s) {
auto copy = std::string(s);
}
void MoveContruct(std::string &&s) {
auto copy = std::move(s);
//auto copy = std::string(std::move(s));
}
int main(int argc, const char *argv[]) {
for (int i = 0; i < 50000000; ++i) {
CopyContruct("hello world");
//MoveContruct("hello world");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编辑:
从这两个函数的汇编中,我可以看到,因为MoveConstruct有一个std::remove_reference类模板的实例化,我认为这应该是罪魁祸首,但我不熟悉汇编,任何人都可以详细说明吗?
以下代码在https://godbolt.org/上使用x86-64 gcc7.2进行了反编译:
CopyContruct(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&):
push rbp
mov …Run Code Online (Sandbox Code Playgroud) 我想在模板类中存储成员函数指针和相关的对象指针task.然后我想创建一个数组并在scheduler类中调用这些成员函数.
但是,如果我将这些tasks 保存在数组中,则它们都需要具有相同的模板类型.这个障碍使得保存任务类中的对象指针毫无用处.
现在,我想知道是否有一种聪明的方法来保护相关调度程序类中的这类对的数组.如果我使用a std::tuple,我不能在运行时通过容器进行迭代,或者至少不能以非常易读的方式进行迭代.有没有解决方案,我不需要调度程序类的模板?
template <class Obj>
struct task {
typedef void (Obj::*task_fn_t)();
Obj* obj_ptr; // object referring
task_fn_t function; // member function
};
template <class Obj>
class scheduler {
task <Obj> *_tasks; // problem :(
};
Run Code Online (Sandbox Code Playgroud)