当前位置: 首页 > news >正文

[]下标的意义

数组:

int array[10];

array[0]获取的是值,还是引用?

std::map<int,string> m_map;

m_map[0]返回的是引用还是值,还是指针。

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>class Test{
public:std::string get_name(int id) const{if (m_map.contains(id)) {return m_map[id];} else {return std::string();}}
private:std::map<int,std::string> m_map{{0,"zero"},{1,"one"}};
};
int main() {Test t;std::cout<<t.get_name(0)<<std::endl;return 0;
}

报错的原因:

函数是const类型,而m_map[id]返回的是引用。

c++编译器为了防止对返回对象的修改,const限定的成员函数内不允许返回引用类型,所以报错。

怎么返回值

map---at()函数

http://www.lryc.cn/news/355571.html

相关文章:

  • 去重复记录和排序——kettle开发09
  • 中创算力与中国移动初步达成战略合作意向,共同构建智能生态圈!
  • 基础—SQL—DML(数据操作语言)插入数据
  • 【改變,是面對的開始】
  • AI大模型实现德语口语练习
  • 一文读懂npm i的命令以及作用
  • You don‘t have enough free space或者no space left on device异常
  • 饮料添加剂新型褪色光照试验仪器太阳光模拟器
  • ElasticSearch - 删除已经设置的认证密码(7.x)
  • 9.4 Go语言入门(运算符)
  • CLIP 源码分析:simple_tokenizer.py
  • AWS安全性身份和合规性之Shield
  • Midjourney入门篇 | 打造最逼真的照片(强烈推荐)
  • 【运维自动化-配置平台】如何跨业务转移主机
  • connection problem,giving up
  • Linux-----sed案例练习
  • 【华为OD机试-C卷D卷-200分】运输时间(C++/Java/Python)
  • flink程序本地运行报: A JNI error has occurred和java.lang.NoClassDefFoundError
  • yolox-何为EMA?
  • Java:String、StringBuffer和StringBuilder的区别
  • 虚拟化技术 分布式资源调度
  • 【Element-plus】vue组合式中使用el-upload通过oss接口上传图片流程(可直接复制使用)
  • C++ 数据结构算法 学习笔记(33) -查找算法及企业级应用
  • 【Linux】在Ubuntu 16.04上安装Gerrit + PostgreSQL + Apache服务
  • 数据倾斜那些事儿
  • python考试成绩管理与分析:从列表到方差
  • Excel某列中有不连续的数据,怎么提取数据到新的列?
  • 翻译《The Old New Thing》- What does it mean when a display change is temporary?
  • 【C语言】char,short char,long char分别是多少字节,多少位,多少bit
  • 新V 系首批订单交付!苏州金龙助新疆游骏文旅集团打造旅运新标杆