File is successfully opened
3 13 34 54 71 81 97 105 116 135 138
I have this data, and I want to call (3,1) then (3,13), then (3,34)..........

How to declare 3 is the first than 13 is second?

{	ifstream stream1 ("STA83STU.txt");
		if ( !stream1 )
		
		{
    	stream1.close (); 
		}
		const int student_size = 12;
		pair<int,int>* array[student_size];
		for (int k = 0; k< student_size; k++)   	
		 
		{
		array[k] = new pair<int,int>(1,k); // Construct each new pair and give its address to an index in the pointer arrayconst int student_size = 12;
		cout <<"("<<"  , k" << ")";
		cout <<"\n"<<endl;
		}

	}

Dani AI

Generated

The posts so far are a bit unclear about intent. showed the line

3 13 34 54 71 81 97 105 116 135 138

and wrote "I want to call (3,1) then (3,13)..." — that looks like a typo. The usual interpretations are either (A) treat the first token (3) as a key and produce pairs (3,13), (3,34), ... or (B) pair the value 3 with index positions (3,1), (3,2), ... Pick A unless the problem really needs indices. asked for a clearer statement; below is a concise, practical way to implement interpretation A without raw pointers or manual memory management.

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <utility>

int main() {
    std::string line = "3 13 34 54 71 81 97 105 116 135 138";
    std::istringstream iss(line);
    int key;
    if (!(iss >> key)) return 0;          // no numbers on the line
    int v;
    std::vector<std::pair<int,int>> pairs;
    while (iss >> v) pairs.emplace_back(key, v);
    for (auto &p : pairs) std::cout << "(" << p.first << "," << p.second << ")\n";
}

Practical notes and alternatives:

  • Read lines from a file with getline and parse each line with istringstream; the same loop scales to multiple rows.
  • Prefer std::vector<std::pair<int,int>> or std::unordered_map<int,std::vector<int>> over arrays of pointers and new/delete. That avoids leaks and is easier to iterate.
  • If the goal is to associate one key with many values, either a multimap or a map-to-vector works; map-to-vector is often clearer when you need to iterate values in insertion order.
  • Troubleshooting: confirm the first token is actually the key (not a count), handle empty lines, and ensure separators (tabs/spaces) are treated by operator>>.

Recommended Answers

All 2 Replies

something like this...

#include <string.h>
#include <iostream>
#include <map>
#include <utility>
using namespace std;
int main()
{  
// Compare (<) function not required since it is built into string class.
  // else declaration would comparison function in multimap definition.  
  // i.e. multimap<string, int, compare> m; 
   multimap<string, int> m; 
    m.insert(pair<string, int>("a", 1)); 
	 m.insert(pair<string, int>("c", 2)); 
	  m.insert(pair<string, int>("b", 3)); 
	   m.insert(pair<string, int>("b", 4)); 
	    m.insert(pair<string, int>("a", 5)); 
		 m.insert(pair<string, int>("b", 6)); 
		  cout << "Number of elements with key a: " << m.count("a") << endl; 
		   cout << "Number of elements with key b: " << m.count("b") << endl; 
		    cout << "Number of elements with key c: " << m.count("c") << endl; 
			 cout << "Elements in m: " << endl;
			   for (multimap<string, int>::iterator it = m.begin(); it != m.end();  ++it)  
			    {      
				 cout <<"["<<(*it).first<<","<<(*it).second<<"]"<<endl;  
				  }  
				   pair<multimap<string, int>::iterator, multimap<string, int>::iterator> ppp;   
				   // equal_range(b) returns pair<iterator,iterator> representing the range  
				    // of element with key b   ppp = m.equal_range("b");   
					// Loop through range of maps of key "b"  
				   cout << endl << "Range of \"b\" elements:" << endl; 
					  for (multimap<string, int>::iterator it2 = ppp.first;  it2 != ppp.second;  ++it2)  
					   {       
					   cout << "  [" << (*it2).first << ", " << (*it2).second << "]" << endl; 
					    }
						// Can't do this (??)
					  //  	 cout << ppp.first << endl;
						 //  cout << ppp.second << endl; 
						   m.clear();
					  }

When are you going to learn to explain what you want in understandable terms? "I want to call (3,1) then (3,13), then (3,34).........." means what? What does "call" mean?

"How to declare 3 is the first than 13 is second?" is also meaningless. The first what? The second what?

Give us a full explanation of what you are trying to accomplish.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.