菜鸟问题,请教VC中 list和Vector的用法。

yixiaojn 2010-07-02 11:07:29
如题,请教VC中 list<CString>和Vector<CString>的用法,包括如何引用头文件和具体使用,如何添加内容和循环输出里面的数据字符串。谢谢!
...全文
529 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
m_tornado 2010-07-02
  • 打赏
  • 举报
回复
ls已经狠详细了,你可以找点stl教程看看,例子也比较多~
雪影 2010-07-02
  • 打赏
  • 举报
回复
#pragma warning (disable:4786)
#include <list>
#include <string>
#include <iostream>

using namespace std ;

typedef list<string> LISTSTR;

// Try each of the four constructors
int main()
{
LISTSTR::iterator i;
LISTSTR test; // default constructor

test.insert(test.end(), "one");
test.insert(test.end(), "two");

LISTSTR test2(test); // construct from another list
LISTSTR test3(3, "three"); // add several <T>'s
LISTSTR test4(++test3.begin(), // add part of another list
test3.end());

// Print them all out

// one two
cout << "test:";
for (i = test.begin(); i != test.end(); ++i)
cout << " " << *i;
cout << endl;

// one two
cout << "test:";
for (i = test2.begin(); i != test2.end(); ++i)
cout << " " << *i;
cout << endl;

// three three three
cout << "test:";
for (i = test3.begin(); i != test3.end(); ++i)
cout << " " << *i;
cout << endl;

// three three
cout << "test:";
for (i = test4.begin(); i != test4.end(); ++i)
cout << " " << *i;
cout << endl;
}
雪影 2010-07-02
  • 打赏
  • 举报
回复
#include <list>
#include <vector>

既然使用了STL了,还用CString? 使用string就可以了
yixiaojn 2010-07-02
  • 打赏
  • 举报
回复
vector::size, vector::capacity, and more (STL Sample)
The sample code below illustrates how to use the vector::size and vector::capacity STL functions in Visual C++.

Required Header:
<vector>

Prototype:


template<class _TYPE, class _A>
void vector::reserve(size_type _N);

template<class _TYPE, class _A>
size_type vector::max_size() const;

template<class _TYPE, class _A>
void vector::resize(size_type _N, _TYPE _X = _TYPE());

template<class _TYPE, class _A>
size_type vector::capacity() const;

template<class _TYPE, class _A>
size_type vector::size() const;


Note: The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability.

Description:
The sample declares an empty vector of integers. It adds a single integer element to the vector, and then shows information about the vector's size, maximum size, and capacity. It reserves storage for 1000 elements and displays the same information about the vector. Finally, it resizes the vector to 2000 elements, and then displays the information.

Sample Code:

//////////////////////////////////////////////////////////////////////
//
// Compile options needed: /GX
//
// Remax.cpp: Illustrates vector::reserve, vector::max_size,
// vector::resize, vector::resize, and vector::capacity.
//
// Functions:
//
// vector::max_size - Returns maximum number of elements vector could
// hold.
//
// vector::capacity - Returns number of elements for which memory has
// been allocated.
//
// vector::size - Returns number of elements in the vector.
//
// vector::resize - Reallocates memory for vector, preserves its
// contents if new size is larger than existing size.
//
// vector::reserve - Allocates elements for vector to ensure a minimum
// size, preserving its contents if the new size is
// larger than existing size.
//
// vector::push_back - Appends (inserts) an element to the end of a
// vector, allocating memory for it if necessary.
//
//////////////////////////////////////////////////////////////////////

// The debugger can't handle symbols more than 255 characters long.
// STL often creates symbols longer than that.
// When symbols are longer than 255 characters, the warning is disabled.
#pragma warning(disable:4786)

#include <iostream>
#include <vector>

using namespace std ;

typedef vector<int> INTVECTOR;

void main()
{
// Dynamically allocated vector begins with 0 elements.
INTVECTOR theVector;

// Add one element to the end of the vector, an int with the value 42.
theVector.push_back(42) ;

// Show statistics about vector.
cout << "theVector's size is: " << theVector.size() << endl;
cout << "theVector's maximum size is: " << theVector.max_size()
<< endl;
cout << "theVector's capacity is: " << theVector.capacity() << endl;

// Ensure there's room for at least 1000 elements.
theVector.reserve(1000);
cout << endl << "After reserving storage for 1000 elements:" << endl;
cout << "theVector's size is: " << theVector.size() << endl;
cout << "theVector's maximum size is: " << theVector.max_size()
<< endl;
cout << "theVector's capacity is: " << theVector.capacity() << endl;

// Ensure there's room for at least 2000 elements.
theVector.resize(2000);
cout << endl << "After resizing storage to 2000 elements:" << endl;
cout << "theVector's size is: " << theVector.size() << endl;
cout << "theVector's maximum size is: " << theVector.max_size()
<< endl;
cout << "theVector's capacity is: " << theVector.capacity() << endl;
}


Program Output is:


theVector's size is: 1
theVector's maximum size is: 1073741823
theVector's capacity is: 1

After reserving storage for 1000 elements:
theVector's size is: 1
theVector's maximum size is: 1073741823
theVector's capacity is: 1000

After resizing storage to 2000 elements:
theVector's size is: 2000
theVector's maximum size is: 1073741823
theVector's capacity is: 2000


--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
yixiaojn 2010-07-02
  • 打赏
  • 举报
回复
谢谢各位

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧