string、数组和vector的关系

C++基础:字符串、 向量和数组 一、命名空间的using声明 1、using声明的基本用法 std: :cin表示从标准输入中读取内容。此处使用作用域操作符(::)的含义是:编译器应从操作符左侧名字所示的作用域中寻找右侧那个名字。因此,std: :cin的意思就是要使用命名空间std中的名字cin。 通过更简单的途径也能使用到命名空间中的成员。本节将学习其中一种最安全的方法,也就是使用using声明(using declaration)。有了using声明就无须专门的前缀(形如命名空间::)也... 阅读详情

可能有很多萌新尝试做一个可以存多个string类型字符串的程序,就是用了数组。运行一看,效果并不是想象的那样。

例如下面这个片段:

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

string a[500];

int main()
{
    for (int i = 1; i <= 500; i++)
    {
        cin >> a[i];
    }
    for (int i = 1; i <= 500; i++)
    {
        cout << a[i] << "\n";
    }
    return 0;
}

这样会发生什么?你根本无法输出正确的500个字符串!举个例子:你输入a[1]时输入的是Hello~,a[2]输入的是Yes,但输出的却是l。因为a[n]查询的是这个字符串的第n+1个字符(因为下标从0开始),输出默认的是a[1]的第3个字符

讲了那么多,我们应该怎么解决呢?

这就要请出我们的vector了

头文件:<vector>

我们可以用这个代码创建string类型的vector数组:

vector<string> a; //a是数组名称,可自行取名

然后我们可以用一个for循环输入字符串到这个数组中

for (int i = 1; i <= 50; i++) //50数值可更改
{
    string l; //创建一个临时存储区
    cin >> l;
    a.push_back(l); //将l中的内容移交给a数组
}

随后我们再通过for循环输出,问题就解决了!

(注:Visual Studio好像不能用,运行会出现未知错误)

完整代码:

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

vector<string> a;

int main()
{
    for (int i = 1; i <= 50; i++)
    {
        string l;
        cin >> l;
        a.push_back(l);
    }
    for (int i = 0; i < a.size(); i++)
    {
        cout << a[i] << "\n";
    }
    return 0;
}

好啦,想要学习到更多C++知识,别忘记关注我哟~

AI大模型实战——模型测评:如何评估大模型的表现 大模型评测体系解析:从数据集到基准测试 本文系统梳理了大模型评测的关键要素。在数据集方面,重点介绍了C-Eval等中文评估套件,强调数据质量需通过人工验证保证。评测维度涵盖自然语言理解、知识推理、代码生成等核心能力。基准测试作为标准化评估方法,Glue、SuperClue等平台提供了公平比较的依据。文章指出厂商自评榜单存在水分问题,建议关注第三方评测。同时解析了少样本和零样本两种prompt模式的应用场景差异。最后解释了"SOTA"这一术语在描述模型最优性能时的应用价值。整体呈现了大模型 阅读详情

相关推荐

CVAT本地部署保姆级教程:用Docker Compose在Ubuntu上5分钟搞定你的私有数据标注平台

本文提供CVAT本地部署的保姆级教程,详细讲解如何在Ubuntu系统上使用Docker Compose快速搭建私有数据标注平台。从环境准备、Docker配置到CVAT部署全流程,涵盖硬件要求、性能优化及安全加固等关键步骤,帮助开发团队5分钟内完成高效、安全的标注环境搭建。

weixin_42619742的博客 383

vector<string>初始化的几种方式

(1)第一种:类似于数组的方式: std::vector<std::string> strArray(10); strArray[0] = "hello"; strArray[1] = "world"; strArray[2] = "this"; strArray[3] = "find"; strArray[4] = "gank"; strArray[5] = "pink"; strArray[6 ]= "that"; strArray[7] = "when"; strArray[8] = "h

喜欢打篮球的普通人 8442

从玩具到工业:手把手教你用Webots仿真Scara抓取(逆解实战篇)

本文详细介绍了如何在Webots仿真环境中实现Scara机械臂的逆解计算与抓取操作,涵盖从环境配置、逆运动学算法到轨迹规划与实时控制的完整流程。通过实战案例演示,帮助读者掌握机械臂逆解的核心技术,并应用于工业自动化场景,提升Scara机械臂的编程与控制能力。

weixin_30932215的博客 504

【c/c++】string、vector和数组

介绍C++中的字符串、vector、数组和迭代器。

jiangwei0512的博客 2667

Cursor+Apifox MCP Server实战:5分钟搞定API自动化测试用例生成

本文详细介绍了如何利用Cursor IDE与Apifox MCP Server,在5分钟内将API文档自动转化为高质量的自动化测试用例。通过配置MCP Server建立数据桥梁,开发者可直接用自然语言指令,让AI基于真实接口规范生成可立即执行的pytest测试代码,大幅提升接口测试效率,特别适合快节奏的SaaS项目。

ooo22的博客 535

解决linux 2.5G网卡驱动问题

解决linux 2.5G网卡驱动问题 关键词:2.5G网卡驱动,ubuntu,安装,2.5G Ethernet LINUX driver,Realtek网卡驱动 由于2.5G网卡较新,各linux发行版的通用驱动已不适用,新装的系统会出现无法上网问题,由于国内网络环境去Realtek官网很难下载到驱动文件,故本文提供github上的2.5G Ethernet LINUX driver r8125 for kernel up to 5.6的版本驱动文件r8125-9.003.05.tar.bz2各位可以自

poorguy_aos的博客 1万+

字符串(string),向量(vector)和数组学习总结

字符串(string),向量(vector)和数组 string和vector是两种最重要的标准库类型,string支持可变长字符串,vector表示可变长集合数组,与之相配套的标准库类型中的迭代器,属于string和vector的配套类型,经常用于访问string中的字符或者vector中的元素 命名空间的using声明 在C++的初步学习之中,我们往往会敲这样的一段代码, using namespace std; 一开始的学习之中,我们也不知道其中的意思,只需要明白照着敲就行 简单来说,到目前为止,我

weixin_51958878的博客 4227

Visual Studio Community与Visual Studio Community与VS Code有啥区别

Visual Studio Code提供了基本的代码编辑、调试和版本控制功能,以及丰富的插件和扩展库。Visual Studio Community和Visual Studio Code在功能上有很大的差异,Visual Studio Community提供了比Visual Studio Code更多的功能,包括代码重构、智能提示、图形化设计等。他的优势在于它的轻量级和灵活性。此外,Visual Studio Code还具有丰富的扩展和插件生态系统,用户可以通过安装插件来扩展其功能,从而满足自己的需求。

m0_56551554的博客 2845

【C++】Vector和String详解

vector和string详解,爆肝万字文只为说明白vector和sting的暧昧关系,以及vector与list之间的分分合合,看完保证你会爱上我!文章涉及容器模拟实现、迭代器失效、深浅拷贝、写时拷贝等多个知识点。

m0_53889456的博客 6322

Neo4j Desktop新手必看:3分钟搞定CSV数据导入(附常见报错解决方案)

本文详细介绍了在Neo4j Desktop中高效导入CSV数据的三种主流方法,包括图形化界面操作、命令行批量导入和Python驱动编程导入,并提供了常见报错解决方案和性能优化技巧,帮助新手快速构建知识图谱。

lambda的博客 649

React项目实战:用AntV G6打造可折叠组织架构图(附完整代码)

本文详细介绍了如何在React项目中使用AntV G6库构建一个功能完整的可折叠组织架构图。通过实战代码演示了从环境搭建、自定义节点、折叠状态管理到性能优化的全流程,重点解决了企业级应用中处理复杂树形数据、实现流畅交互等核心问题,并提供了可直接复用的组件代码。

weixin_29016315的博客 313

【C++】 vector <string> 的超详细解析!!!

在【C++】中,vector是一个动态数组,它可以根据需要自动调整自身的大小。同样 vector 表示一个由 string 组成的 vector 。string 是一个字符序列,可以理解为文本字符串。但是在平时刷 leetcode 的时候,还是搞不清楚 vector的用法 ,所以通过查阅资料,将其记录下来。

weixin_45031801的博客 1万+

SAP ABAP 接口管理服务(ZIF)开源项目介绍

接口管理服务(ZIF)开源项目基于 SAP ABAP系统,是一款对标SAP PI/PO 的轻量接口管理产品。他通过独立开发包部署在ABAP系统中,包含接口注册、接口管理、日志服务及其他配套功能,拥有丰富的技术沉淀并通过市场检验,目前已被多家企业应用在SAP ERP系统中。

SAP李斌,本人承诺所有原创文章均不使用任何AI生成技术,请大家放心阅读。 1746

C++ Primer 字符串、向量和数组

文章目录命名空间的using声明标准库类型string定义和初始化string对象直接初始化和拷贝初始化string对象上的操作读写string对象比较string对象字面值和string对象相加处理字符基于for语句处理每个字符串基于for语句改变每个字符串标准库类型vector定义和初始化vector对象 命名空间的using声明 作用域操作符::的含义是:编译器从操作符左侧名字所示的作用域中寻找右侧那个名字。 //有using声明,就无须专门前缀也能使用所需的名字 //格式为:using namesp

weixin_46365033的博客 224

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

C++_Chapter 3 数组、vector与字符串

Chapter 3 数组、vector与字符串 3-1 数组的引入 # include <iostream> # include <type_traits> int main() { int a;//int int b[10]; //int[10] std::cout << std::is_same_v<decltype(b),int[10]> << '\n';//输出为1 } int x; std::cin >> x

satisfiedmin的博客 425

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

《c++ primer》第三章 字符串、vector、数组

本章内容相比第二章要简单不少,里面比较重要的内容主要是vector和迭代器,这里只是很简单的介绍了一下,在后续的章节会有更详细、复杂的说明。以下记录的都是比较重要或者易混淆的知识点,对于像string、vector只列举了部分方法的例子。

qq_18824403的博客 974

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

C++ 数组 详解

数组是一种复合类型。声明形式:a[d],其中a是数组的名字,d是数组的维度。维度说的就是数组中元素的个数,必须大于0。数组中元素的个数也属于数组类型的一部分,编译的时候维度应该是已知的。所以维度必须是一个常量表达式。//不是常量表达式。//常量表达式,关于constexper。//含有10个整数的数组。//含有42个整型指针的数组。//错误:cnt不是常量表达式。//当get_size是constexper时正确。默认情况下,数组的元素被默认初始化。

WWester的博客 879

【C++学习】——(五)数组

【C++学习】——(五)数组 开篇 上一篇讲解了类型,通过类型来开始本篇的学习; int a[10]; 上述代码中的a是什么类型呢? 相信很多人都知道是一个数组类型,具体来说是一个int[10]的类型; 数组概念 定义:将一到多个相同对象串连到一起,所组成的类型; 初始化方式: 1、缺省初始化:int x[5]; 2、聚合初始化:int x[] = {1,2,3}; 注意: 1、不能用auto来声明数组类型; 2、数组不能复制,也不能赋值; 数组的复杂声明 指针数组的声明: int *i[5]; 大家思

weixin_40620310的博客 1954

c++ 字符串向量数组

string、vector、数组

watermelon的博客 1640

《C++ Primer》阅读笔记(三)字符串、向量和数组

第3章 字符串、向量和数组 命名空间的using声明(Namespace using Declarations) 使用using声明后就无须再通过专门的前缀去获取所需的名字了。 using std::cout; 程序中使用的每个名字都需要用独立的using声明引入。 头文件中通常不应该包含using声明。 ...

ben1010101010的博客 429

C++ 基础笔记:string、vector、数组与迭代器

/ 维度必须是常量表达式// 不是常量表达式(运行时才确定)// 常量表达式// ❌ 错误!cnt 不是常量表达式// ✅ 正确// ✅ 正确,字面量是常量表达式// 列表初始化(维度可省略,编译器自动推断)// 维度为 3// {0, 1, 2, 0, 0},剩余补 0// ❌ 错误!初始值过多// 函数体内的数组:默认初始化包含未定义的值int a[3];// ⚠️ 包含垃圾值,不是 0!// ✅ 全部初始化为 0// 3 行 4 列的二维数组。

Ckillmeno的博客 410

C++Primer学习(3.5 数组)

由内向外的顺序可帮助我们更好地理解Parray的含义:首先是圆括号括起来的部分,*Parray意味着Parray是个指针,接下来观察右边,可知道Parray是个指向大小为10的数组的指针,最后观察左边,知道数组中的元素是int。同理,(&arrRef)表示 arrRef是一个引用,它引用的对象是一个大小为10的数组,数组中元素的类型是int。这里显然使用下标运算符索引了一个不存在的元素,arr有10个元素,尾元素所在位置的索引是9,接下来那个不存在的元素唯一的用处就是提供其地址用于初始化e。

wgf350357114的博客 1065

【C++学习】基础知识:字符串、vector、迭代器和数组

C++基础知识:字符串、向量、迭代器和数组

weixin_42659457的博客 257

C++primer第五版 字符串、向量和数组重要知识点

1. 标准库类型string (1)string对象上的操作 string对象会自动忽略开头的空白(即空格符、换行符、制表符等)并从第一个真正的字符开始读起,直到遇见下一处空白位置。 (2)使用getline读取一整行 getline函数的参数是一个输入流和一个string对象,函数从给定的输入流中读入内容,直到遇到换行符为止(注意换行符也被读进来了),然后把所读的内容存入到那个strin...

elma_tww的博客 341

C++知识总结(2)--字符串和数组

标准库类型string 标准库类型string表示可变长的字符序列。 使用string类型需要先添加头文件#include<string>,并且由于其定义在命名空间std中,所以还要添加using std::string;。string初始化的方式有下面几种方式:其中使用等号的初始化执行的是拷贝初始化,是将等号右侧的初始值拷贝到新创建的对象中;而不使用等号,执行的就是直接初始化。下面给出了str

AI 算法笔记 895

string vector

如果什么都不写,直接调用T temp = a;// 完整拷贝整个字符串,复制堆上所有字符,低效a = b;b = temp;性能极差,违背 swap 轻量化设计。类内成员 swap// 写在你的自定义string类里面只要有这个成员函数,调用时,编译器依靠 ADL 查找规则,优先调用该函数,会自动执行,编译器优先去bit命名空间、string类内调用模拟实现的成员函数 swap,和标准文档描述行为一致,只交换三个内置变量,O (1) 开销。

2401_86976959的博客 231
上一篇: C++如何清除屏幕上的内容?
下一篇: 自制一个输入网址就能打开网站的程序
「已注销」
博客等级 码龄4年 45粉丝 17原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值