小编use*_*118的帖子

结合map和c ++字符串数组

我正在编写一个代码来检查一个人之前是否访问过某个地方,如果没有,那么除了将新地点添加到访问地点列表中我将使用地图将学生名称存储为密钥和数组存储地点如下

#include<map>
using namespace std;

map<string,string[]> stu_plac;
map<string,string[])::iterator it;
Run Code Online (Sandbox Code Playgroud)

我无法找到在地图上搜索的roght方式

我尝试了以下方法:

bool exist=false;
if(stu_plac.count(name))
{
    it=stu_plac.find(name);
    for(auto tt=(*it).second.begin(); tt!=(*it).second.end(); tt++)
    {
        if (tt.compare(place)==0)//exist
        {
            exist=true;
        }
        break;
    }
    if (!exist)
    {
        (*it)second[++tt]=place;
    }
}
else
{
    stu_plac.insert(pair<string,string[]>(name,place));
}
Run Code Online (Sandbox Code Playgroud)

我认为问题在于字符串迭代器的数组,请你帮我找到正确的方法来做到这一点.我是否需要使用multimap或其他数据结构来执行此操作?

c++ arrays map

4
推荐指数
1
解决办法
3464
查看次数

如何通过指针访问向量?

我想vector使用指针插入新元素我有以下示例代码:

struct info {
    string Name;
    int places;  // i will use the binary value to identfy the visited places example 29 is 100101
                 // this means he visited three places (London,LA,Rome)  
    vector<int> times; // will represent the visiting time,e.g. 1,2,5 means london 1 time, LA
                       // twice and Rome five times
};

map<string,vector<info> *> log;
Run Code Online (Sandbox Code Playgroud)

Peaple来自不同的城市,我将检查城市是否存在,只需将新人添加到其中vector,否则创建一个新的地图对象:

vector<info> tp;
info tmp;
if(log.size()==0|| log.count(city)==0) //empty or not exist 
{
    tp.push_back(tmp);
    vector<info>* ss = new vector<info>;
    ss=&(tp);
    // …
Run Code Online (Sandbox Code Playgroud)

c++ pointers stdmap stdvector

1
推荐指数
1
解决办法
160
查看次数

标签 统计

c++ ×2

arrays ×1

map ×1

pointers ×1

stdmap ×1

stdvector ×1