ACCESS attributes to fill MULTIMAP

Write your question here.

1
2
3
4
5
  struct source{
string AA BB CC;
source(std::string p1="MARK",std::string p2="21 VEGAS", std::string p3="0645454545") :
AA(p1), BB(p2), CC(p3) {}
}

1
2
3
4
5
6
7
class BASE{
public;
BASE(BASE& w,source& z);
friend class child;
private:
string ZZ;
source N;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class child:public BASE
{
public:
child ();
///MY PROBLEM is how access attributes of source and BASE to use as parameters //to fill my multimap
void ADD(BASE& a){
    multimap <BASE, string> abo;
    typedef pair <BASE,string> duo;
    typedef multimap < BASE,string> ::iterator it;
    //////////////////////////////::
///ALL OF THIS DIDN'T WORK FOR ME
         //  abo.insert({ a.ZZ, a.N });
 //   abo.insert({ a.ZZ, a.N});
  // BASE(a.ZZ,a.N) =  abo[a.ZZ] ;
 //  abo[BASE(a.ZZ,a.N)]=a.ZZ;

}
}
Last edited on
Why does it feel like that you had a different account, which you have removed (and its posts with it)?
http://www.cplusplus.com/forum/beginner/276505/
yes it is you are right i'm that rabbit so please i'm struggling please i need a help i'm struggling four one week
EXEMLPE:
abo[BASE(a.ZZ,a.N)] = abo(a.ZZ);
/src/children.cpp:32:40: error: no match for call to ‘(std::multimap<BASE, const std::__cxx11::basic_string<char> >) (const string&)’
   32 |     abo[BASE(a.ZZ,a.N )] = abo(a.ZZ);
The abo has type std::multimap<BASE,std::string>
What does the interface of multimap provide? See http://www.cplusplus.com/reference/map/multimap/

* There is no operator[]. Therefore, you can't call abo[x]
* There is no operator(). Therefore, you can't use abo as functor. The abo(y) is not possible. The error message says that.
* For adding elements there is insert(). See http://www.cplusplus.com/reference/map/multimap/insert/
Hence: abo.insert(z);
What type must the z have?

iterator multimap::insert (const value_type& val);
In your case the value_type is duo (aka std::pair<BASE,std::string>)

How does one create a duo object? You need a BASE object and string object:
1
2
3
BASE a;
string w;
abo.insert( duo(a,w) );

You should know yourself what string you want. Your Add() function gets only a BASE as argument.

Note:
Internally, the elements in a multimap are always sorted by its key following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).

For your multimap the Compare is less<BASE>.

Therefore, this must be possible:
1
2
BASE a, b;
if ( a < b ) 

For that you need bool operator< ( const BASE&, const BASE& );
Is this necessary because my MULTIMAP have two defferents costum parameters?if so what is the implementation of operator operator< and operator== in this case.
THE AIM IS TO HAVE AN ALPHABETICAL ORDER

Thank
Is this necessary because my MULTIMAP have two defferents costum parameters?

Which two parameters?

1
2
3
4
5
bool operator< ( const BASE& lhs, const BASE& rhs )
{
  // return true, if lhs is before rhs
  // else return false
}

Multimap does not use operator==.
But excuse me what do you mean by before rhs exmple: strcmp() ??????
because here we cope with string.
If lhs is before rsh (the way you want to order them), then lhs<rhs is true and rhs<lhs is false.

You want alphabetical order "by string". Your BASE has four strings.

std::string has operator< see http://www.cplusplus.com/reference/string/string/operators/
this is my implimentation it stores witch means abo.insert( duo(a ,a.info) ) worked for me
but when i wanna see the contains by using iterator it does not compile
1
2
3
4
5
6
7
8
multimap < BASE,source> abo;
    typedef pair < BASE,source> duo;
    typedef multimap < BASE,source> ::iterator it;
    abo.insert( duo(a ,a.info) );/////worked
    cout<<"--------------------"<<abo.size()<<endl;/////1 

       for(it=abo.begin(); it!=abo.end(); ++it){////////////HERE IS MY PROBLEM
        cout << it->first << " => " << it->second << '\n';

COMPILING ERROR:
 error: expected unqualified-id before ‘=’ token for(it=abo.begin(); it!=abo.end(); ++it){

here is my overloalind < code
1
2
3
4
5
6
bool operator< ( const BASE& a ,const BASE& b )
	     {
    if ( a.nom < b.nom )
	return false;
	else return true;
      }
OVERLOADING WORKS I THINK
Last edited on
1
2
typedef multimap < BASE,source> ::iterator it;
for ( it = abo.begin(); it != abo.end(); ++it )

What is it?

Do you write other loops like:
for ( int = 0; int < 7; ++int )
or:
for ( int x = 0; x < 7; ++x )

Last edited on
Yes for sure int is type not a variable but i tried to understand what do you mean .
All i know is "it" is an iterator pointer whereby we can navigate through containers of a multimap.

EXCUSE ME///
Unfortunately it doesn't work here it is the output:
error: no match for ‘operator<<’ (operand types are ‘std::ostream’ {aka ‘std::basic_ostream<char>’} and ‘source’)cout << it->second<<"----" << endl;
      |             ~~~~ ^~ ~~~~~~~~~~
      |             |           |
      |             |          source
      |             std::ostream {aka std::basic_ostream<char>}


the implementation is
1
2
3
for(multimap <BASE,source> ::iterator it=abo.begin(); it!=abo.end(); ++it){////////////HERE IS MY PROBLEM
            cout << it->first<<"----" << endl;
            cout << it->second<<"----" << endl;



The aime of this is to create a Base class containing name as a private string and a struct containing lastname,adresse, phone but BASE class contains "source info " as private attributes so we want to store those atributes in nient we want to use Base as a key wich can reprsent name i don't know if this is
convinient <BASE,source> i dont know may be this is the best <BASE,string>
Last edited on
The error message says that problem is in cout << it->second<<"----" << endl;
You would get the same error with:
1
2
source data;
std::cout << data; // error: How to do this? 

You have not defined std::ostream& operator<< ( std::ostream&, const source& );
yes it is done but unfortunately that wasn't the expected .
What is expected is to have as first ->abo only name of subscriber as a key and in the other hand of the map i 'm supposed to have the information of that subscriber and what i have as out put looks like this .
|WILLIS|HOLLYWOOD|0645454545
----
JIM|9AVENUE|9900900898
////while i want a name in the key i know i can manage the things by changing the key as type string and pointing a.nom but the use of BASE as a key is a MUST .

this is the constractor
1
2
3
4
5
6
7
8
9
10
11
BASE:BASE ( std::string& name,source& r)
// Algorithme :
//
{


  name =this->nom ;
   info=r.prenom+"|"+r.adresse+"|"+r.noTelephone;


}
Last edited on
i think the a part of problem is here abo.insert( duo(a ,a.info) ) we can't point a.name in the first parameter because it will be type string and i want BASE to be my key as a name of subscriber.
Think of a table with two columns.
The rows sorted by the first column.
The values in the first column are unique; no same value twice.
The second column has no such restrictions.
Some languages call such table "dictionary".

Why do you have the data in map at all? When you can find a BASE, you can already read its 'info'. You don't need to repeat the 'info' in separate column.
I think of a second a class and a MAP for further functions.but the problem is solved now and thank you so much .Now my problem is when i have a string forme and i want to split it by specific character.
my form looks like this string S=" name|lastname|adresse|phone"
I used getline (ss ,line,'|') so i think we should have four strings.what i want to do is to split it by only two elements and by the first '|' met.so the string will be in two parts rather than four like before string1= name string2 =lastname|adresse|phone.
i'm stucked.
If you call getline (ss ,line,'|') once, you do get string1, don't you?
All the rest is string2.

Or, you split the input to multiple strings and then concatenate all but first back to string2.
no no no how i'm i stupid thank you so much ,it was a pleasure
Topic archived. No new replies allowed.