惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

M
MIT News - Artificial intelligence
博客园 - Franky
H
Help Net Security
A
About on SuperTechFans
Know Your Adversary
Know Your Adversary
罗磊的独立博客
Help Net Security
Help Net Security
腾讯CDC
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
Project Zero
Project Zero
有赞技术团队
有赞技术团队
Blog — PlanetScale
Blog — PlanetScale
T
Threat Research - Cisco Blogs
The Hacker News
The Hacker News
Engineering at Meta
Engineering at Meta
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
Google DeepMind News
Google DeepMind News
V
V2EX
B
Blog
人人都是产品经理
人人都是产品经理
J
Java Code Geeks
N
Netflix TechBlog - Medium
P
Privacy International News Feed
Recorded Future
Recorded Future
D
Darknet – Hacking Tools, Hacker News & Cyber Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Stack Overflow Blog
Stack Overflow Blog
Cisco Talos Blog
Cisco Talos Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Securelist
NISL@THU
NISL@THU
The GitHub Blog
The GitHub Blog
T
Troy Hunt's Blog
S
Security @ Cisco Blogs
Vercel News
Vercel News
L
LINUX DO - 热门话题
博客园_首页
The Register - Security
The Register - Security
GbyAI
GbyAI
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V2EX - 技术
V2EX - 技术
L
LangChain Blog
T
Tor Project blog
P
Privacy & Cybersecurity Law Blog
Security Latest
Security Latest
K
Kaspersky official blog

博客园 - 周伟

域名解析文件hosts文件是什么?如何修改hosts文件? PowerDesigner 使用心得 实现TOMCAT服务下一个ip绑定多域名绑定的方法 CSV扩展类 C++导入导出CSV文件 struct+vector实现树结构 WebService 随写 mysql相关文章收集 Linux下两种自动启动Tomcat的方法 linux自动启动mysql LINUX下mysql的大小写区分设置 Jdom使用指南 UML用例中使用和扩展意义 枚举和字符串互转 单件模式的C++标准实现 设计模式 Add Crash Reporting to Your Applications with the CrashRpt Library 设计模式——单键模式(singleton)C++实现 Singleton的C++实现 及相关问题
vector+struct在数据接口中的应用
周伟 · 2009-06-05 · via 博客园 - 周伟

开发的时候,经常需要设计数据列表的接口,有时数据列表个数又是可变的。

1.UserInfo.h

#pragma once
#include <vector>
using std::vector;
typedef struct _stuUserInfo
{
 char UserName[20];
 char CNUserName[20];
 long UserID;
}UserInfo;
class CUserInfo
{
public:
 CUserInfo(void);
 virtual ~CUserInfo(void);
 BOOL GetUserInfo(vector<UserInfo> &vUserInfo);
};
2.UserInfo.cpp

BOOL CUserInfo::GetUserInfo(vector<UserInfo> &pvUserInfo)
{
 UserInfo stuUserInfo;
 memset(&stuUserInfo,0,sizeof(UserInfo));
 CString strUserName;
 CString strCNUserName;
 long nUserID = 0;
 
 for (long i=0;i<12;i++)
 {
  strUserName.Format(_T("Izhouwei%d"),i);
  strCNUserName.Format(_T("周伟%d"),i);
  strcpy_s(stuUserInfo.CNUserName,strUserName.GetBuffer(0));
  strcpy_s(stuUserInfo.UserName,strCNUserName.GetBuffer(0));
  stuUserInfo.UserID = i;

  if (i % 3 == 0)
  {
   pvUserInfo.push_back(stuUserInfo); 
  }
  else
  {
   pvUserInfo.insert(pvUserInfo.begin(),stuUserInfo);
  }
 }

 vector<UserInfo> vUserInfoTemp(1);
 vUserInfoTemp[0] = pvUserInfo[11];
 pvUserInfo[11] = pvUserInfo[1];
 pvUserInfo[1] = vUserInfoTemp[0];
 return TRUE;
 
}

3.调用过程

void CTest001Dlg::OnBnClickedButton1()
{
 vector<UserInfo> vUserInfo;
 CUserInfo userInfo;
 userInfo.GetUserInfo(vUserInfo);
 CString strMessage = NULL;

//采用下标的方式来读取数据
 for (vector<int>::size_type ix = 0;ix <vUserInfo.size();ix++)
 {
  strMessage.Format(_T("%s;%s;%d"),vUserInfo[ix].UserName,vUserInfo[ix].CNUserName,vUserInfo[ix].UserID);
  MessageBox(strMessage);
 }

//采用迭代器的方式循环

 for (vector<UserInfo>::iterator it=vUserInfo.begin();it!=vUserInfo.end();it++)
 {
  strMessage.Format(_T("%s;%s;%d"),(*it).CNUserName,(*it).UserName,(*it).UserID);
  MessageBox(strMessage);
 }
}