Asssociative arrays: How to add data?

Hi, I have the following problem:
Text file "strlines.dat" contains series of the following information of straight lines:
1) coordinate of one line's point
2) angle slope
It looks like (x,y) 45 deg:

2,6 60
4,7 45
2,1 30

After adding this information to associative container verify it and find lines
1) with same coordinates
2) with same angles
Showing results separately for each 1) and 2).
How to add info from file to associative container and find it? It should be like this?:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <string.h>
#include <iostream>
#include <map>
#include <utility>

typedef std::pair<std::(int, int), std::int>  strlines;

int main(){
   // straight line is defined by: 
   //  	1) coordinate of one line's point
   //	2) angle slope

   map<int, string>  Line; // associative array
   map<int,string>::iterator it; // iterator for access to Lines

  Line.insert(strlines.dat);

 for(it= Line.begin(); it!= Line.end(); ++it){
	       cout <<"Point's coordinate: : "<< (*it).first << " Angle slope of line: " << (*it).second << endl;

// Verify if coordinates of some points are identical
  if((it=Line.find()) == Line.end()){
    std::cout<<"Point's coordinate: "<< (*it).first<<" Angle slope of line: " << (*it).second << endl;
 } else{
    
    cout << "No lines with similar coordinates"; 
 }

return 0;
}

Last edited on
Have you even tried to compile this?

Line 6: You don't put std:: in front of ints. std::pair only takes two arguments.

Line 13: Why is your map map<int,string> ? Don;t you want the key to your container to be a coordinate? A single int is not a 2D coordinate and the angle is not a string.

Line 16: strlines is a typedef, not an instance of a pair. What is dat? It is not defines anywhere and is not a member of pair.

Line 22: Your condition is reversed. If the iterator equals end(), then there are no lines with that coordinate.
This is corrected code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string.h>
#include <iostream>
#include <map>
#include <utility>

typedef std::pair<std::(int, int), std::int>  strlines;

int main(){
   // straight line is defined by: 
   //  	1) coordinate of one line's point
   //	2) angle slope

   map<char, int>  Line; // associative array
   map<char, int>::iterator it; // iterator for access to Lines

  Line.insert(strlines.dat);

 for(it= Line.begin(); it!= Line.end(); ++it){
	       cout <<"Point's coordinate: : "<< (*it).first << " Angle slope of line: " << (*it).second << endl;

// Verify if coordinates of some points are same
  if((it=Line.find()) == Line.end()){
    std::cout<<"Point's coordinates is not in the map" << endl;
 } else{
 
    cout << "Lines with identicl coordinates are exist"; 
 }
return 0;
}

How to add info from the file "strlines.dat" to associative container and find it?
Last edited on
Still won't compile. You haven't fixed the errors in line 6 and 16 that I pointed out before.

If strlines.dat is a data file, lines 16 isn't going to magicly insert a data file into your map. You need to open the data file and read from it.

Line 13: Why did you change the map to map<char,int> ? That's still not going to represent a coordinate and an angle. A coordinate is a x and y value.

Line 18: You have an opening brace for the for loop, but no closing brace.
I've compiled this starting-up code (My stationary comp was damaged at the previous time):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string.h>
#include <iostream>
#include <map>
#include <utility>

typedef std::pair<std::int, std::int>  strlines;

int main(){
   // straight line is defined by: 
   //  	1) coordinate of one line's point
   //	2) angle slope

   map<int, int>  Line; // associative array
   map<int, int>::iterator it; // iterator for access to Lines

  Line.insert(strlines.dat);

 for(it= Line.begin(); it!= Line.end(); ++it){
	       cout <<"Point's coordinate: : "<< (*it).first << " Angle slope of line: " << (*it).second << endl;

// Verify if coordinates of some points are same
  if((it=Line.find()) == Line.end()){
    std::cout<<"Point's coordinate is not in the map"<< endl;
 } else{
    
    cout << "Lines with similar coordinates are exist"; 
 }
 return 0;
}

And Builder show the following errors:
-------------------------------------------------------------------------------------
Build
[C++ Error] 02.CPP(6): E2272 Identifier expected
[C++ Error] 02.CPP(6): E2272 Identifier expected
[C++ Error] 02.CPP(13): E2451 Undefined symbol 'map'
[C++ Error] 02.CPP(13): E2188 Expression syntax
[C++ Error] 02.CPP(14): E2188 Expression syntax
[C++ Error] 02.CPP(16): E2451 Undefined symbol 'Line'
[C++ Error] 02.CPP(16): E2108 Improper use of typedef 'strlines'
[C++ Error] 02.CPP(18): E2451 Undefined symbol 'it'
[C++ Error] 02.CPP(19): E2451 Undefined symbol 'cout'
[C++ Error] 02.CPP(19): E2451 Undefined symbol 'endl'
[C++ Error] 02.CPP(29): E2134 Compound statement missing }
Last edited on
You're really not paying attention to all the things AbstractionAnon is telling you, are you?
I've tried to edit code by AbstractionAnon's recommendations with the exclusion of Line 6 because until only read about typedef by the web.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <string.h>
#include <iostream>
#include <map>
#include <utility>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <conio.h>

void outputLine (int, int, int)

typedef std::pair<std::int, std::int>;

int main(){
   // straight line is defined by:
   //  	1) coordinate of one line's point
   //	2) angle slope

   ifstream inDataFile("strlines.dat", ios::in);
     if (!inDataFile)
       {
         cout << "File cannot be opened\n";
         exit(1);
         }

   int x;
   int y;
   int angle;

   while (inDataFile >> x >> y >> angle)
       outputLine (x, y, angle);

   map<int, int, int>  Line; // associative array
   map<int, int, int>::iterator it; // iterator for access to Lines


  // Line.insert(strlines.dat); How to add info to associative container.

 for (it= Line.begin(); it != Line.end(); ++it){
	       cout <<"Point's coordinate: : "<< (*it).first << " Angle slope of line: " << (*it).second << endl;
               }

// Verify if coordinates of some points are same
  if((it=Line.find()) == Line.end()){
    std::cout<<"Point's coordinate is not in the map"<< endl;
 } else{

    cout << "Lines with similar coordinates are exist";
 }
 return 0;
}

void outputLine (int x1, int y1, int angle1)
{
  cout << setiosflags(ios::left) << setw(10) << x1 << y1 << angle1 << endl;
  }


Errors:
Line 12: [C++ Error] 02.CPP(12): E2141 Declaration syntax error
Line 44: [C++ Error] 02.CPP(44): E2285 Could not find a match for 'map<int,int,int,allocator<pair<const int,int> > >::find()'

Thanks for recommendations that decrease amount of errors. But program don't add info to the "strlines.dat".
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <fstream>
#include <map>
#include <iostream>
#include <iomanip>

using namespace std;

//  An X,Y coordinate specification
typedef std::pair<int,int> Point;

void outputLine (Point, int);

int main()
{   ifstream inDataFile("strlines.dat", ios::in);
    if (!inDataFile)
    {   cout << "File cannot be opened\n";
        exit(1);
    }
    Point point;
    int angle;
    map<Point, int>  lines;				// associative array
    map<Point, int>::const_iterator it; // iterator for access to Lines

    while (inDataFile >> point.first >> point.second >> angle)
    {   pair<Point, int> element (point, angle);	//  what to insert		
        lines.insert (eelement);   //	Do the insert
        outputLine (point, angle);
    }
	
    for (it= lines.begin(); it != lines.end(); ++it)
    {   cout << "Point's coordinate: : " << it->first.first << "," << it->first.second 
             << " Angle slope of line: " << it->second << endl;
    }
    return 0;
}

void outputLine (Point pt, int angle1)
{   cout << setiosflags(ios::left) << setw(10) << pt.first << pt.second << " " << angle1 << endl;
} 


I leave it to you to search for duplicates.

Note that since this is a map with Point as the key, there will not be duplicate points even if the angles are different. If you want to allow multiple points with the same x,y values but different angles, you would want to use std::multimap.


Topic archived. No new replies allowed.