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

推荐订阅源

F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Vercel News
Vercel News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
Check Point Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
The Hacker News
The Hacker News
L
LINUX DO - 热门话题
T
Tenable Blog
Hugging Face - Blog
Hugging Face - Blog
Google Online Security Blog
Google Online Security Blog
博客园 - Franky
P
Proofpoint News Feed
H
Hacker News: Front Page
P
Privacy & Cybersecurity Law Blog
月光博客
月光博客
P
Proofpoint News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
博客园_首页
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
Forbes - Security
Forbes - Security
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
博客园 - 叶小钗
T
Threat Research - Cisco Blogs
aimingoo的专栏
aimingoo的专栏
D
Darknet – Hacking Tools, Hacker News & Cyber Security
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Hacker News - Newest:
Hacker News - Newest: "LLM"
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
O
OpenAI News
G
Google Developers Blog
Martin Fowler
Martin Fowler
罗磊的独立博客
S
SegmentFault 最新的问题
T
Tor Project 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);
 }
}