473,734 Members | 2,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::map<int, std::string[2]> problems

First of all, forgive me if this is the wrong place to ask this question,
if it's a stupid question (it's my second week with C++), or if this is
answered some place else (I've searched but not found anything).

Here's the problem, I have two sets of files, the name of a file contains a
number which is unique for each set but it's possible (even probable) that
two files in different sets have the same numbers. I want to store these
file-names in such a way that I can retrieve them by their number. For this
I thought I'd use a map with the number as the key an an array of strings
to store the file-names, however I've run into trouble when trying to use
this solution.

When adding the file-names to the map I don't know if a filename from the
other set already exists, but given examples at:
http://www.fredosaurus.com/notes-cpp.../map/pair.html
http://www.fredosaurus.com/notes-cpp...-wordfreq.html
it seams like entries not found are created and if they are found I can
modify them.
Here's code that displays my problem:

#include <map>
#include <string>
#include <vector>

using std::map;
using std::string;
using std::vector;

class Files
{
public:
void nextFile(string *, string*);
void open(vector<str ing>*, int);

private:
int getNumber(strin g); // Gets the number from the filename

map<int, string[2]> fileSets;
map<int, string[2]>::iterator iter;
};

// Return the next pair of files
// I've removed a bit of code to keep it short,
// among other things checks for fileSets.end().
void Files::nextFile (string* file1, string* file2)
{
iter++;
// Return the pointers to the filenames
file1 = &(iter->second[0]);
file2 = &(iter->second[1]);
}
// Add files to the sets
// files is a vector with the filenames to be added.
// setNr is the number of the set the files should be
// added to, can be 0 or 1.
void Files::open(vec tor<string>* files, int setNr)
{
if(setNr < 2)
{
for(int i = 0; i < files->size(); i++)
{
// Add the file to the correct set
int fileNumber = getNumber((*fil es)[i]);
(fileSets[fileNumber])[setNr] = (*files)[i];
// This does not work ^^^^^^^
}
}

// Set the iterator
iter = fileSets.begin( );
}

------------------------------------------

When compiling on gcc 3.3 I get the following:

/usr/include/c++/3.3.4/bits/stl_map.h: In member function `_Tp& std::map<_Key,
_Tp, _Compare, _Alloc>::operat or[](const _Key&) [with _Key = int, _Tp =
std::string[2], _Compare = std::less<int>, _Alloc =
std::allocator< std::pair<const int, std::string[2]> >]':
Files.cpp:46: instantiated from here
/usr/include/c++/3.3.4/bits/stl_map.h:319: error: ISO C++ forbids casting to an
array type `std::string[2]'

------------------------------------------
and I get similar results when using gcc 3.4. Obviously there's some problem
with the array but I can't figure out what and would appreciate a nudge in the
right direction, and since I'm still learning any other comment too.

PS:
It's not possible to get random access to a map if the key is not known
but if I have an (bidirectional) iterator pointing at some element in the
map, and know that the element I'm interested in is N elements from the one
the iterator is currently pointing at can I jump to that element by adding
N to the iterator, e.g. iter+=N ?

--
Erik Wikström
Jul 23 '05 #1
19 6160
Erik Wikström wrote:
First of all, forgive me if this is the wrong place to ask this question,
It's not. You're fine.
if it's a stupid question (it's my second week with C++), or if this is
answered some place else (I've searched but not found anything).
It may have been answered already, but sometimes it's rather hard to find.
[...]

map<int, string[2]> fileSets;
You simply can't do that. Arrays cannot be the stored data in a standard
container. If you need two strings there, either make a pair (std::pair)
or make your own struct.
[...]


V
Jul 23 '05 #2

"Erik Wikström" <er***********@ telia.com> schrieb im Newsbeitrag
news:XO******** ***********@new sb.telia.net...
Here's the problem, I have two sets of files, the name of a file contains
a
number which is unique for each set but it's possible (even probable) that
two files in different sets have the same numbers. I want to store these
file-names in such a way that I can retrieve them by their number. For
this
I thought I'd use a map with the number as the key an an array of strings
to store the file-names, however I've run into trouble when trying to use
this solution.
As Victor already explained, you cannot use a C style array as the value
type of a standard container. But you could use an std:: vector instead.
However, as you describe your problem, you have two sets of files, not one
set of pairs of files. So I would suggest to use two instances of
std::map<int, std::string>, one for each set of files.
PS:
It's not possible to get random access to a map if the key is not known
but if I have an (bidirectional) iterator pointing at some element in the
map, and know that the element I'm interested in is N elements from the
one
the iterator is currently pointing at can I jump to that element by adding
N to the iterator, e.g. iter+=N ?


Have a look at std::advance.

HTH
Heinz
Jul 23 '05 #3
> map<int, string[2]> fileSets;

string[2] has no = operator and no < operator - that's the whole
problem.
-Gernot
Jul 23 '05 #4
Victor Bazarov wrote:
Erik Wikström wrote:
map<int, string[2]> fileSets;

You simply can't do that. Arrays cannot be the stored data
in a standard container. If you need two strings there,
either make a pair (std::pair)


But in the case in question, the array did not constitute
the "stored data". The "stored data" elements in the map
are actually of type std::pair<int, std::string[2]>.
What's wrong with that?
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #5
"Gernot Frisch" <Me@Privacy.net > wrote in message
news:3h******** ****@individual .net...
map<int, string[2]> fileSets;


string[2] has no = operator and no < operator - that's the whole
problem.
-Gernot


The comparison in the original post was being done on type int,
which DOES have "=" and "<" operators. std::map contains elements
of type std::pair<key_t ype, value_type>. Only the key_type
needs to be "comparable ", as i understand it.

For example, the following program compiles fine:

#include <iostream>
#include <string>
#include <map>
#include <utility>
using std::cout;
using std::endl;
using std::string;
using std::map;
using std::pair;
using std::make_pair;
int main(void)
{
string Splat[2] = {"apple", "peach"};
map<int, string[2]> fileSets;
fileSets.insert (make_pair<int, string[2]>(37, Splat));
return 0;
}

However, when I try to run it, I get a general protection fault.
What am I doing wrong? I don't think it has to do with the
non-comparibility of std::string[2] , though. GPF usually
means illegal memory usage somewhere. (Possibly a problem
with the copy semantics of std::string[2]?)
Puzzled,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #6

"Robbie Hatley" <lonewolfintj at pacbell dot net> schrieb im
Newsbeitrag news:42******** **@spool9-west.superfeed. net...
"Gernot Frisch" <Me@Privacy.net > wrote in message
news:3h******** ****@individual .net...
> map<int, string[2]> fileSets;


string[2] has no = operator and no < operator - that's the whole
problem.
-Gernot


The comparison in the original post was being done on type int,
which DOES have "=" and "<" operators. std::map contains elements
of type std::pair<key_t ype, value_type>. Only the key_type
needs to be "comparable ", as i understand it.

For example, the following program compiles fine:

#include <iostream>
#include <string>
#include <map>
#include <utility>
using std::cout;
using std::endl;
using std::string;
using std::map;
using std::pair;
using std::make_pair;
int main(void)
{
string Splat[2] = {"apple", "peach"};
map<int, string[2]> fileSets;
fileSets.insert (make_pair<int, string[2]>(37, Splat));
return 0;
}

However, when I try to run it, I get a general protection fault.
What am I doing wrong? I don't think it has to do with the
non-comparibility of std::string[2] , though. GPF usually
means illegal memory usage somewhere. (Possibly a problem
with the copy semantics of std::string[2]?)


The second argument must either have an = operator or an cctor - I'm
not sure, I think it't the = op, which string[2] does not offer.
Jul 23 '05 #7
Gernot Frisch wrote:
The second argument must either have an = operator or an cctor - I'm
not sure, I think it't the = op, which string[2] does not offer.


For each STL container holds that the value type must be assignable and copy
constructable (23.1.3).

But why does this code compile?

Mathias
Jul 23 '05 #8
I suspect that string[2] decays to a pointer to its first element.

Dan

Jul 23 '05 #9
Well, actually, I tried the code on VC7.1 and it doesn't compile
(cannot create the pair because it cannot find the appropriate copy
constructor for the type string[2]).
I was thinking that it compiles because, in the process of construction
of the contained element of type string[2], it converts the array into
a pointer to its first element and it copies the pointer, hence the GPF
he gets.

Your explanation doesn't explain the GPF he gets.

Dan

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6716
by: gipsy boy | last post by:
// -- using namespace std; map<string,string> formMap; list<string> formParams; for(list<string>::iterator fit = formParams.begin(); fit!=formParams.end(); fit++) { cout << "key=" << *fit; formMap.insert(make_pair(*fit,*(++fit)));
5
8748
by: Peter Jansson | last post by:
Hello, I have the following code: std::map<int,std::set<std::string> > k; k="1234567890"; k="2345678901"; //... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k) throw(std::runtime_error)
13
4632
by: jstanforth | last post by:
This is probably a very obvious question, but I'm not clear on what operators need to be implemented for std::map.find() to work. For example, I have a class MyString that wraps std::string, and which also implements ==, <, <=, >, >=, etc. (Those operators are tested and working correctly.) If I assign map = "world", it saves the MyString's correctly in the map. But a subsequent call to map.find("hello") returns map.end(). Even more...
1
3755
by: Maxwell | last post by:
Hello, I having having oodles of trouble using the std lib in my MC++ (VS.NET 2003) Class library. I figured out a simple sample to reproduce the errors I am having. Create a MC++ (VS.NET 2003) class library and type in the following code below: #include <map> #include<string>
4
11719
by: lada77 | last post by:
All, Just wondering if one of you very helpful guru's out there could comment on some odd behaviour I am seeing. I'm betting it is something obvious but I am not experienced enough to tell right away. Here is my code snippet and the results that I am seeing: #include <map> #include <iostream> int
0
1940
by: mirkmmd | last post by:
Hi I am new to this site and also to c++. The problem I am facing is that I when I run the following code, I expect to get '"Today is sunny" at the end of the program. And I do get it in Visual Studio but not in Linux(g++ 4.1.0, Fedora). What I get in Linux is "Todayissunny". I actually need the whole string with spaces in Linux to be a part of some other functions. Can anybody tell me where do the spaces go? #include<string> #include...
2
2327
by: darkkal | last post by:
Hi I'm a student majoring a computer . I tried to make a simple hashing table like (int) 1 , (std::string) "One" There is a problem in using std::map ㅜㅜ this is a simple code. PLZ~~Help me :) #include <iostream>
2
2789
by: jabbah | last post by:
I have some data in a map and I want to sort it. Currently I have implemented it like this: #include <iostream> #include <map> #include <string> using namespace std; int main(){
0
8776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.