RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-469749

Никита Джосан's questions

Martin Hope
Никита
Asked: 2023-12-29 22:13:48 +0000 UTC

正确的运算符重载 [] (C++)

  • 9

我面临着编写自己的关联数组并为其重载索引运算符 [] 的任务。我写的,重载看起来像这样:

template<class Key, class Data>
Data& AssociativeArray<Key, Data>::operator[](const Key& key)
{
    for (Pair& p : array)
    {
        if (p.key == key)
        {
            return p.value;
        }
    }

    // Если ключ не найден создаём новую пару
    Pair newPair = { key, Data{} };
    array.push_back(newPair);
    return array.back().value;
}

//для константного массива
template<class Key, class Data>
const Data& AssociativeArray<Key, Data>::operator[](const Key& key) const
{
    for (const Pair& p : array)
    {
        if (p.key == key)
        {
            return p.value;
        }
    }
    return Data{};
}

我认为我的问题已经很明显了:在我的实现中,我寻找一个键,如果它不存在,我用这样的键创建一个新的空元素。一方面,这很方便 - 这样我可以分配一个新值,就像常规数组一样:

Arr["newKey"] = "new value";

但另一方面,当尝试从不存在的键中获取值时

std::string str = Arr["newKey"];

它创建一个新的空的,虽然它不应该,但应该抛出一个错误。

所以问题是:我怎样才能正确地做到这一点,以便它在接收值时抛出错误,并在添加它时创建一个元素?

以防万一,这是整个类结构:

template<class Key, class Data>
class AssociativeArray
{
public:
    Data& operator[](const Key& key);
    const Data& operator[](const Key& key) const;
    template<class K, class D>
    friend std::ostream& operator<<(std::ostream& os, const AssociativeArray<K, D>& arr);

private:
    struct Pair
    {
        Key key;
        Data value;
    };
    std::vector<Pair> array;
};

template<class Key, class Data>
Data& AssociativeArray<Key, Data>::operator[](const Key& key)
{
    for (Pair& p : array)
    {
        if (p.key == key)
        {
            return p.value;
        }
    }

    // Если ключ не найден создаём новую пару
    Pair newPair = { key, Data{} };
    array.push_back(newPair);
    return array.back().value;
}

template<class Key, class Data>
const Data& AssociativeArray<Key, Data>::operator[](const Key& key) const
{
    for (const Pair& p : array)
    {
        if (p.key == key)
        {
            return p.value;
        }
    }
    return Data{};
}

template<class Key, class Data>
std::ostream& operator<<(std::ostream& os, const AssociativeArray<Key, Data>& arr)
{
    for (const auto& pair : arr.array)
    {
        os << "Key: " << pair.key << ", Value: " << pair.value << std::endl;
    }
    return os;
}
c++
  • 2 个回答
  • 83 Views
Martin Hope
Никита Джосан
Asked: 2023-04-08 21:08:09 +0000 UTC

brcmsmac自动启动

  • 5

我在我的旧笔记本电脑上安装了 2 个操作系统:首先是 Windows 7,然后在同一个磁盘上,在它旁边,我安装了 Ubuntu。这是我第一次使用 Linux。要连接 Wi-fi,您必须在每次启动时运行驱动程序:

~$ sudo modprobe brcmsmac

但是每次开始都不方便。是否有自动执行此操作的选项?我在网上找不到答案

linux
  • 1 个回答
  • 12 Views
Martin Hope
Никита Джосан
Asked: 2023-03-12 04:08:17 +0000 UTC

如何使用 graphics.h 在 Turbo C++ 中正确编译程序?

  • 7

写了下面的代码:

    #include <graphics.h>
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
       /* request auto detection */
       int gdriver = DETECT, gmode, errorcode;
       int midx, midy, midx2, midy2;
       int radius = 25;
    
       /* initialize graphics and local variables */
       initgraph(&gdriver, &gmode, "c:/turboc3/bgi");
    
       /* read result of initialization */
       errorcode = graphresult();
       if (errorcode != grOk)  /* an error occurred */
       {
          printf("Graphics error: %s\n", grapherrormsg(errorcode));
          printf("Press any key to halt:");
          getch();
          exit(1); /* terminate with an error code */
       }
    
       midx = midx2 = getmaxx() / 2;
       midy = midy2 = getmaxy() / 2;
       int i = 0;
       setfillstyle(1, 3);
       while (!kbhit())
       {
        cleardevice();
    
        setcolor(RED);
        setfillstyle(XHATCH_FILL, RED);
        fillellipse(midx2, midy2, radius, radius);
    
        setcolor(GREEN);
        setfillstyle(XHATCH_FILL, GREEN);
        fillellipse(midx, midy, radius, radius);
    
        setcolor(WHITE);
        line(midx2, midy2, midx, midy);
    
        i = getch();
        switch(i)
        {
            case 115: midy += 2; break;
            case 119: midy -= 2; break;
            case 97: midx -= 2; break;
            case 100: midx += 2; break;
            default: return 0;
        }
       }
    
    
       /* clean up */
       getch();
       closegraph();
       return 0;
    }

编译,编译,收到一个.exe文件。当通过 Turbo C++ (ctrl + F9) 启动时,一切正常,显示,绘制。当我运行 exe 文件本身时,我看到了下图:一个空的控制台,行的开头有一个闪烁的光标:没有绘制任何内容,没有从键盘输入任何内容(exe 文件被扔进了 Source 文件夹编译后我没有把它从那里拉出来)。 空控制台

我爬上了英文论坛,了解甚少,但问题似乎在于 Turbo C 制作的 16 位程序不再在 64 位上运行。但!2要点:第一,我是32位的Windows 7,其次,论坛上有人说他在编译一个32位的程序(但没有解释如何)。

简而言之,问题是:我做错了什么,为什么什么都没画,如何解决?

Ps 我知道该程序已过时,有 sfml 等,但这个特殊时刻对我来说很有趣。

c++
  • 1 个回答
  • 18 Views
Martin Hope
Никита Джосан
Asked: 2022-04-10 22:03:57 +0000 UTC

我找不到 Python 语法错误,或者我使用了错误的东西

  • 0

为了在实践中学习 Python,对于一个不和谐的机器人,我正在尝试制作一个命令,该命令应该在聊天中显示嵌入消息,而用户自己输入他想在此消息中看到的内容

理论上,用户应该输入,例如: !!em 'Title': 'Заголовок', 'Desc': 'Текст' 然后除了命令之外的所有内容都应该写成一个数组(不是在单独的文件中,而是在编译时只在内存中),如下所示:

embedCache {
'Title': 'Заголовок',
'Desc': 'Текст'
}

这是代码本身:

@bot.command()
async def em(ctx, *, textE): #звёздочка в скобках указывает, что весь текст сообщения будет значением переменной textE
    embedCache = {eval(textE)}
    embedVar = discord.Embed(title=embedCache['Title'], description=embedCache['Desc'])
    await ctx.send(embed=embedVar)

但是,它在第​​一行的数组中给了我一个语法错误(我不知道具体是哪一个,没有指出,因为数组没有写在任何地方)

错误画面:(对不起,我不能输入错误,因为它很大,我不知道你需要输入什么,我想你自己很无聊)

在此处输入图像描述

python
  • 1 个回答
  • 10 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5