小编son*_*yao的帖子

在结构矢量中搜索

我有一个带有结构的向量看起来像这样

struct person_t
{

    string name;
    string id;
    struct location_t location;    
};

    vector <person_t> myvector;
Run Code Online (Sandbox Code Playgroud)

我已经阅读了myvector中的项目

但现在我需要知道如何在向量中搜索特定项目并计算它在向量中的项目数量.

c++ search struct vector

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

什么!s || !*是什么意思?(s是char*)

我很难理解这部分意味着什么:

 if (!s || !*s) //What is this?!?!
      {
        printf("\n");
        return;
      }
Run Code Online (Sandbox Code Playgroud)

这是我的主要功能:

#include <conio.h>
#include <stdio.h>
#include <string.h>

void func1(char *s);

int main()
{
    func1("SABABA");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

而我的func1:

void func1(char *s)
{
    int a, b, c;
    if (!s || !*s)
    {
        printf("\n");
        return;
    }
    b = strlen(s);
    while (b>2)
    {
        a = 0;
        if (s[a] == s[b - 1])
        {
            for (c = b - 1; c >= a && s[a] == s[c]; a++, c--);
            if (c<a)
            { …
Run Code Online (Sandbox Code Playgroud)

c string

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

我需要将所有内容都放在 C++ 的类中吗?

我被告知 C++ 中的所有内容(即使它没有被实例化,但在多个地方使用)都必须放在一个类中。尽管我知道我不必这样做,但我不知道哪个更糟糕:创建一堆不会被实例化的类,或者在 main.cpp 中拥有一堆函数。

如果我按照别人告诉我的去做,这里有一些示例代码:

#include <iostream>
using namespace std;

int main(){
// line of code to call class that acts as a calculator
}
Run Code Online (Sandbox Code Playgroud)

c++ class function

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

该对象具有与成员函数C ++不兼容的类型限定符

std::ostream& operator<<(std::ostream&, const Course&);

void Course::display() {
    std::cout << std::left << courseCode_ << " | " << std::setw(20) << courseTitle_ << " | " << std::right
        << std::setw(6) << credits_ << " | " << std::setw(4) << studyLoad_ << " | ";
}

std::ostream& operator<<(std::ostream& os, const Course& a) {
    a.display();
    return os;
}
Run Code Online (Sandbox Code Playgroud)

问题在下面的ostream运算符的实现中发生a.display()。我看不出问题出在哪里,我还有其他代码可用于相同的实现。

错误信息:

该对象的类型限定符与成员函数“ sict :: Course :: display”不兼容。对象类型为const sict :: Course

c++ iostream operator-overloading member-functions

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

如何使各种高度相似的类型的函数通用?

我有一个链接列表格式,不同类型的(int,double,例如):

struct DblNode {
    double value;
    DblNode * next;
}
struct IntNode {
    int value;
    IntNode * next;
}
Run Code Online (Sandbox Code Playgroud)

现在我正在为这些列表做些事情,我遇到的问题是我不断复制和粘贴函数,进行次要类型编辑:

DblNode * dbl_listind(DblNode * head,int ind){
    DblNode * position = head;
    int counter = 0;
    while(counter < ind){
        position = position -> next;
        counter++;
    }
    return position;
}
Run Code Online (Sandbox Code Playgroud)

然后重复int.

有没有办法以某种方式具有通用列表类型,然后以某种方式指定此功能,独立于我的链表的值成员的类型?

c++ templates c++11

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

如何通过引用推送向量?

下面的代码不起作用,因为我将矢量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
查看次数