Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6059407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:42:25+00:00 2026-05-23T08:42:25+00:00

I have an array of strings which need to be taken into a map.

  • 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:42:25+00:00Added an answer on May 23, 2026 at 8:42 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of strings . I need all the strings which do
I have a two-dimensional array (of Strings) which make up my data table (of
I have a .NET string which is Base64 encoded representation of an array of
I have an array of 1000 strings to load into a combo box. What
I have a string which was converted to a char array and stored in
i have a JavaScript array of string arrays: array1; array2 ; array3 which i
I have a Session variable which is an array of integers, for example: myArray{1,4,3,5,6,7,9,2,...n}
I have a SQL table with column which contain string like 'type|type1|type2|type3|type4'. I need
I have written an if...else statement which uses an array myArr and a string
I have an array of 300K strings which represent dates: date_array = [ 2007-03-25

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.