将字符串从映射转换为二维向量字符方式

发布于 2024-11-16 07:09:03 字数 1299 浏览 1 评论 0原文

我有一个字符串数组需要放入地图中。由于数组大小是可变的,我需要一个二维向量来按字符获取字符串。我需要这两种存储格式来对它们执行操作。这是我的尝试..在(编辑:)运行时出现错误。

#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
#include<map>
#include<vector>
#include<algorithm>
#include<iterator>

#define rep(i,a,b) for(int i=(a);i<=(b);i++)

using namespace std;
std::map<int,string>col;
std::map<int,string>row;
std::map<int,string>::iterator p;     
std::map<int,string>d1; 
std::map<int,string>d2;

int main()
{   
    int i=0,r=0;
    string s;

    ifstream ip; 
    ip.open("a.in"); 

    ofstream op; 
    op.open("a_out.in");

    ip>>s;

    const int c= s.length();
    ip.seekg(0,std::ios::beg);

    do { 
        ip>>s;row.insert(make_pair(r,s));
        r++;
    }while(s.length()==c);

    p=row.find(--r);
    row.erase(p);
    p = row.begin();

    while(p!=row.end())
    {
        cout<<(p->first)<<","<<(p->second)<<"\n";
        p++;
    }

    vector<vector<char>>matrix(r,vector<char>(c)); 

    rep(i,0,r){
        int k=0;rep(j,0,c)(p->second).copy(&matrix[i][j],1,k++);
    }

    rep(i,0,r)
        rep(j,0,c)
            cout<<matrix[i][j];
return 0;
}

I have an array of strings which need to be taken into a map. Since the array size is variable, I need a 2d vector to obtain the string character-wise. i need both formats of storage for operations i perform on them. Here's my attemp..gives errors in (EDIT:)run time.

#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
#include<map>
#include<vector>
#include<algorithm>
#include<iterator>

#define rep(i,a,b) for(int i=(a);i<=(b);i++)

using namespace std;
std::map<int,string>col;
std::map<int,string>row;
std::map<int,string>::iterator p;     
std::map<int,string>d1; 
std::map<int,string>d2;

int main()
{   
    int i=0,r=0;
    string s;

    ifstream ip; 
    ip.open("a.in"); 

    ofstream op; 
    op.open("a_out.in");

    ip>>s;

    const int c= s.length();
    ip.seekg(0,std::ios::beg);

    do { 
        ip>>s;row.insert(make_pair(r,s));
        r++;
    }while(s.length()==c);

    p=row.find(--r);
    row.erase(p);
    p = row.begin();

    while(p!=row.end())
    {
        cout<<(p->first)<<","<<(p->second)<<"\n";
        p++;
    }

    vector<vector<char>>matrix(r,vector<char>(c)); 

    rep(i,0,r){
        int k=0;rep(j,0,c)(p->second).copy(&matrix[i][j],1,k++);
    }

    rep(i,0,r)
        rep(j,0,c)
            cout<<matrix[i][j];
return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然#的苍凉 2024-11-23 07:09:03

看起来问题是在打印出地图之后、将字符串复制到向量中之前发生的。您需要两件事:

while(p!=row.end())
{
    cout<<(p->first)<<","<<(p->second)<<"\n";
    p++;
}
p = row.begin();   // Must reset iterator!

vector<vector<char>>matrix(r,vector<char>(c));
rep(i,0,r){
    int k=0;
    rep(j,0,c)(p->second).copy(&matrix[i][j],1,k++);
    ++p; // Must advance the iterator.
}

这应该修复映射/集合迭代器不可取消引用的问题,因为在双重嵌套的 for 循环中您引用了无效的迭代器(p 被设置为 row.end())。

编辑:
另外,除非您可以假设所有字符串的长度相同,否则您可能会考虑采用不同的技术。当您使用 const int c = s.length() 时,您是在告诉 mapvector文件中每个字符串的长度都完全相同。如果第二个字符串比第一个字符串短,您将尝试访问字符串中不存在的字符!请注意,

rep(j,0,c) (p->second).copy(&matrix[i][j],1,k++)

它将失败,因为它认为它具有 c 字符,而实际上不会。

It looks like the problem occurs after you print out the map, before you copy the strings into the vector. You need two things:

while(p!=row.end())
{
    cout<<(p->first)<<","<<(p->second)<<"\n";
    p++;
}
p = row.begin();   // Must reset iterator!

vector<vector<char>>matrix(r,vector<char>(c));
rep(i,0,r){
    int k=0;
    rep(j,0,c)(p->second).copy(&matrix[i][j],1,k++);
    ++p; // Must advance the iterator.
}

That should fix the map/set iterator not dereferencable, as in the doubly nested for loop you referenced an invalid iterator (p was set to row.end()).

Edit:
Also, unless you can assume that all the strings are the same length, you might consider a different technique. When you use const int c = s.length(), you are telling the map<int,string> and vector<char> the length of EVERY string in your file are the exact same length. If the second string is shorter than the first, you will try to access characters in the string that don't exist! Note the

rep(j,0,c) (p->second).copy(&matrix[i][j],1,k++)

will fail since it thinks it has c characters, when it in fact will not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文