小编son*_*yao的帖子

为什么它只收到1个整数?

为什么只接收一个整数?
这是代码:

#include <iostream>

int main () {
    int num1,num2,num3;
    std::cin>>num1,num2,num3;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ operator-precedence comma-operator

0
推荐指数
1
解决办法
41
查看次数

如何通过引用推送向量?

下面的代码不起作用,因为我将矢量a和b推回到矢量矢量,然后改变矢量a和b.我想改变矢量a和b,以便矢量矢量遭受相同的修改.我该怎么做呢?

#include <iostream>
#include <vector>

int main()
{
std::vector<std::vector<int>>vector;
std::vector<int>a;
std::vector<int>b;
vector.push_back(a);
vector.push_back(b);
for (int i = 1; i <= 10; i++)
    a.push_back(i);
for (int i = 11; i <= 20; i++)
    b.push_back(i);
std::cout << vector[1][0];
std::cin.get();
}
Run Code Online (Sandbox Code Playgroud)

c++ reference vector

0
推荐指数
2
解决办法
146
查看次数

为什么我不能用nullptr初始化自动推导的指针?

我试图做类似的事情:

auto foo = int*(nullptr);
Run Code Online (Sandbox Code Playgroud)

哪个VC++不能编译错误消息:

不允许输入名称

并且GCC 8.2不能编译:

'int'之前的预期主表达式

我真的很好奇为什么这似乎是一种非法的语法.在我看来它应该没问题,因为文字可以像这样初始化.

auto foo = int(2);
Run Code Online (Sandbox Code Playgroud)

我能想到让它工作的唯一方法是创建一个类型别名或执行此操作:

auto foo = std::add_pointer_t<int>(nullptr);
Run Code Online (Sandbox Code Playgroud)

我试着谷歌搜索,但坦率地说,我甚至不知道如何正确地提出这个问题,因为我的标准很弱.任何见解将不胜感激!

c++ syntax casting

0
推荐指数
2
解决办法
100
查看次数

使用&符号命名的变量后跟下划线是什么意思?

我正在研究C ++,发现了这种奇怪的语法。这&_是什么意思,为什么作者可以将该变量用作_1st

std::sort(open_list.begin(), open_list.end(), [] (const auto &_1st, const auto &_2st) {
  return _1st->h_value + _1st->g_value < _2st->h_value + _2st->g_value
});
Run Code Online (Sandbox Code Playgroud)

编辑1

我认为,要创建变量的引用,必须使用&运算符,后跟名称,例如:&_1st(注意它们之间的空白)。

下划线是在C ++中声明名称变量的一种特殊方法吗?

c++ reference c++17

0
推荐指数
1
解决办法
153
查看次数

错误:“x”不是类型,其中 x 是构造函数参数

class Example {
    //...
    static auto make = []() -> std::shared_ptr<receiveObject> {
        return std::make_shared<receiveObject>(Params::EchoBufferSize);
    };

    static auto recycle = [](std::shared_ptr<receiveObject> o) {
        //nothing for now
    };
    recycle::shared_pool<receiveObject> receivePool(make, recycler);
};
Run Code Online (Sandbox Code Playgroud)

但我得到

In file included from /home/project/AIPTCPClient.cpp:57:0:
/home/project/AIPTCPClient.h:312:57: error: 'make' is not a type
         recycle::shared_pool<receiveObject> receivePool(make, recycler);
                                                         ^~~~
/home/project/AIPTCPClient.h:312:63: error: 'recycler' is not a type
         recycle::shared_pool<receiveObject> receivePool(make, recycler);
                                                               ^~~~~~~~
Run Code Online (Sandbox Code Playgroud)

正如您在shared_pool中看到的,它需要两个函数,这就是我传递 lambda 的原因。但 C++ 抱怨它们应该是类型。我不明白。为什么构造函数需要类型而不是对象?

c++ c++11

0
推荐指数
1
解决办法
499
查看次数

cout of string 不显示任何内容

encrypted字符串的 cout不显示任何内容,有时程序会崩溃。当我做cout << encrypted[i]for 循环时,我得到了正确的结果。此外,如果我执行 for 循环以按字符读取字符串字符for(char c:encrypted)cout << c << endl;=> 它也不起作用并且得到了垃圾。

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string key = "XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr";
    string encrypted;
    string decrypted;
    string message;
    int pos{};
    cout << "enter the message" << endl;
    getline(cin,message);


    //encrypting
    for (size_t i{} ;i<message.length();i++)
        {   
            if (alphabet.find(message[i]) != string::npos)
            {pos = alphabet.find(message[i]);        

            encrypted[i] = key[pos];                    

            }else
             {encrypted[i]=message[i];
                cout << encrypted[i];
             }
        }

    cout << "the …
Run Code Online (Sandbox Code Playgroud)

c++ string

0
推荐指数
1
解决办法
68
查看次数

std::string 隐式转换优先级,string_view 优于 const char*

我有两个重载函数:

void Set(const char * str) { std::cout << "const char * setter: " << str << "\n"; }
void Set(const std::string_view & sv) { std::cout << "string_view setter: " << sv << "\n"; }
Run Code Online (Sandbox Code Playgroud)

我在一个std::string. 它选择std::string_view重载而不是 const char * 重载,即它选择std::stringto std::string_viewover std::stringto的隐式转换const char *

这是在 中的保证行为std::string,还是他们的选择是任意的?如果这是一个有保证的行为,他们是如何使它更喜欢 string_view 转换而不是另一个?

c++ stl overload-resolution implicit-conversion c++17

0
推荐指数
1
解决办法
89
查看次数

以错误的方式初始化 string_view 的向量?

你好,

请告诉我为什么前 3 行不起作用而后 3 行起作用?

见:https : //gcc.godbolt.org/z/qozEsc

#include <vector>
#include <string_view>
#include <iostream>

int main(){

    const std::vector<std::string> W = {"aaa", "bbbb", "ccccc"};
    
    std::vector<std::string_view> Wbak1;
    for (std::string w : W) Wbak1.push_back(w.c_str());
    for(auto w:Wbak1) std::cout << w << '\n';

    std::vector<std::string_view> Wbak2;
    for (auto i = 0U; i < W.size(); ++i) Wbak2.push_back(W[i].c_str());
    for(auto w:Wbak2) std::cout << w << '\n';
}
Run Code Online (Sandbox Code Playgroud)

最好的问候, 40tude

c++ string vector view c++17

0
推荐指数
1
解决办法
167
查看次数

错误:'运营商^'不匹配

如何在字符串中按位xor?谁能帮我这个...

#include <bits/stdc++.h>
using namespace std;

int main() {
    // your code goes here

    string a="1001";
    string b="1111";
    string c=a^b;
    cout << "c: " << c << "\n";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误:'operator ^'不匹配(操作数类型是'std :: __ cxx11 :: string {aka std :: __ cxx11 :: basic_string}'和'std :: __ cxx11 :: string {aka std :: __ cxx11 :: basic_string }')string c = a ^ b;

c++ string

-1
推荐指数
1
解决办法
802
查看次数

如何初始化vector <char>的向量?

我有来自数独拼图的数据.我必须在下面定义一些内容vector<vector<char>>.

[[".", "8", "7", "6", "5", "4", "3", "2", "1"], 
 ["2", ".", ".", ".", ".", ".", ".", ".", "."],
 ["3", ".", ".", ".", ".", ".", ".", ".", "."], 
 ["4", ".", ".", ".", ".", ".", ".", ".", "."],
 ["5", ".", ".", ".", ".", ".", ".", ".", "."], 
 ["6", ".", ".", ".", ".", ".", ".", ".", "."], 
 ["7", ".", ".", ".", ".", ".", ".", ".", "."], 
 ["8", ".", ".", ".", ".", ".", ".", ".", "."], 
 ["9", ".", ".", ".", ".", …
Run Code Online (Sandbox Code Playgroud)

c++ initialization vector

-1
推荐指数
1
解决办法
444
查看次数