问题描述
基本上,我需要为这个数组中的每个条目存储一个字符串和一个数据结构。它需要能够变得更大(但我不会从
中删除它)。另外,我需要能够以某种方式将元素存储在这个数组中。
Basicly, I need to store a string plus a data structure for each entry in
this array. It needs to be able to get bigger (but I wont be deleting from
it). Also, I need to be able to store the elements in this array somehow.
推荐答案
C ++标准库中的''vector''类应该满足你的需求。
这是一个使用字符串向量的简单示例。你可以通过定义一个类或结构来保存你想要的东西来概括它,并且
制作一个矢量。
#include< vector>
#include< string>
#include< iostream>
using namespace std ;
int main()
{
vector< string> foo2的; //以零长度向量开始
foo2.push_back(" Hello,"); //向量自动展开
foo2.push_back(" my");
foo2.push_back(" name");
foo2 .push_back("是");
foo2.push_back(" Munich。");
//找出向量有多大通过使用size()成员函数。
for(int k = 0; k< foo2.size(); ++ k)
cout << foo2 [k]<< " " ;;
cout<<结束;
返回0;
}
-
Jon Bell < JT ******* @ presby.edu> Presbyterian College
美国南卡罗来纳州克林顿物理与计算机科学系
The ''vector'' class in the C++ standard library should fill your needs.
Here''s a simple example that uses a vector of strings. You can
generalize it by defining a class or struct to hold whatever you want, and
making a vector of that.
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main ()
{
vector<string> foo2; // start with a zero-length vector
foo2.push_back("Hello,"); // vector expands automatically
foo2.push_back("my");
foo2.push_back("name");
foo2.push_back("is");
foo2.push_back("Munich.");
// find out how big the vector is by using the size() member function.
for (int k = 0; k < foo2.size(); ++k)
cout << foo2[k] << " ";
cout << endl;
return 0;
}
--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
vector< pair< string,yourdatastructure> >
我不确定你的意思是我需要能够以某种方式存储这个数组中的元素
。
Victor
vector<pair<string,yourdatastructure> >
I am not sure what you mean by "I need to be able to store the elements
in this array somehow".
Victor
我的意思是排序不存储。
可以轻易地以某种方式对向量中的元素进行排序吗?
I meant sort not store.
Can one easliy sort the elements in a vector somehow?
这篇关于是否有可动态调整大小的数组的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!