将字符串从映射转换为二维向量字符方式
我有一个字符串数组需要放入地图中。由于数组大小是可变的,我需要一个二维向量来按字符获取字符串。我需要这两种存储格式来对它们执行操作。这是我的尝试..在(编辑:)运行时出现错误。
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来问题是在打印出地图之后、将字符串复制到向量中之前发生的。您需要两件事:
这应该修复映射/集合迭代器不可取消引用的问题,因为在双重嵌套的 for 循环中您引用了无效的迭代器(p 被设置为 row.end())。
编辑:
另外,除非您可以假设所有字符串的长度相同,否则您可能会考虑采用不同的技术。当您使用
const int c = s.length()
时,您是在告诉map
和vector
文件中每个字符串的长度都完全相同。如果第二个字符串比第一个字符串短,您将尝试访问字符串中不存在的字符!请注意,它将失败,因为它认为它具有
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:
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 themap<int,string>
andvector<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 thewill fail since it thinks it has
c
characters, when it in fact will not.