is there a built in function to deal with numbers similar to how the swap member function (first.swap(second);) deals with strings? thanks

Recommended Answers

All 13 Replies

that is a template and works with strings as well as integers.

int main()
{
    string a = "Hello";
    string b = "World";
    swap(a,b);
    cout << a << " " << b << "\n";
    return 0;
}

>is there a built in function to deal with numbers similar to how the swap member function
Yes, include <algorithm> and you can use the std::swap template function as AD has shown.

Narue its a part of utility.h in VS2008 and in Utility.h and xstring on VS2005.

>Narue its a part of utility.h in VS2008 and in Utility.h and xstring on VS2005.
That's nice. And your point is?

then what is wrong with these..

swap(average[i], average[min]);
			   swap(LNs[i], LNs[min]);        
			   swap(FNs[i], FNs[min]);
			   swap(Scores[i][0], Scores[min]);
			   swap(Scores[i][1], Scores[min]);
			   swap(Scores[i][2], Scores[min]);
			   swap(Scores[i][3], Scores[min]);
			   swap(coursegr[i], coursegr[min]);

FN's & LN's are strings.. rest are floats

>swap(Scores[0], Scores[min]);
>swap(Scores[1], Scores[min]);
>swap(Scores[2], Scores[min]);
>swap(Scores[3], Scores[min]);
This strikes me as a type mismatch. Most likely you're trying to swap a float with an array of floats, and the compiler doesn't like it.

>swap(Scores[0], Scores[min]);
>swap(Scores[1], Scores[min]);
>swap(Scores[2], Scores[min]);
>swap(Scores[3], Scores[min]);
This strikes me as a type mismatch. Most likely you're trying to swap a float with an array of floats, and the compiler doesn't like it.

i changed that too swap(Scores[3], Scores[min][3]); etc but for all of it i am still getting these errors (or something similar for each swap:
example:

swap(average, average[min]);

error C2563: mismatch in formal parameter list

error C2784: 'void std::swap(std::basic_string<_Elem,_Traits,_Alloc> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'float'

why is it giving me such a painful problem when i'm simply trying to swap data out of one array of floats with data from another array of floats

Post a complete (small!) program that exhibits the problem, please.

i'm taking the input (Input function: http://www.daniweb.com/forums/post576243.html#post576243) i read-in and alphabetizing it into alphabetical order. i realize i could go the simple route of swapping like
int temp = IDs;
IDs = IDs[i+1];
IDs[i+1]=temp;
however i'd like to understand swap() and for what reason it's not working out here.
sample function:


//** ******* **
void LNSort(char*(FNs)[], char*(LNs)[], float(Scores)[][4], int nRecords) {
//GIVEN: Three ARRAYS containing relevant information & a index variable.
//TASK: Function used to sort in alphabetical order & output scores based on Last Names (A-Z).
//RETURNS: All ARRAYS in sorted order relative to each other using swap().

    for(int k=0; k<(nRecords-1); k++)
	{
	   int min=k;
	   for(int j=k+1; j<nRecords; j++)
            if(LNs[j]<LNs[min])
               min = j;

			   swap(LNs[k], LNs[min]);        
			   swap(FNs[k], FNs[min]);
			   swap(Scores[k][0], Scores[min][0]);
			   swap(Scores[k][1], Scores[min][1]);
			   swap(Scores[k][2], Scores[min][2]);
			   swap(Scores[k][3], Scores[min][3]);    }

	ofstream output("output.txt", ios::out | ios::app);
	if(output.is_open())
	{
		//alot of output
	}
}

example errors:

swap(average, average[min]);

error C2563: mismatch in formal parameter list

error C2784: 'void std::swap(std::basic_string<_Elem,_Traits,_Alloc> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_string<_Elem,_Traits,_Alloc> &' from 'float'

swap(LNs, LNs[min]);
error C2563: mismatch in formal parameter list
error C2568: '
it basically does something similar to that for every swap() etc.

is there some simple #include i'm missing or something?

I included <algorithm> and <fstream> (because what you posted is not a complete program) and added using namespace std; . No errors, and by the looks of the error, you're using a version of Visual Studio, which is what I tested on.

i've had all of those declarations, still getting these errors.

#include <iomanip>   
#include <iostream>
#include <fstream>
#include <algorithm>
#define SIZE 50
using namespace std;

void GetData(char[], char[], float[][4], int &);
void PrintData(char[], char[], float[][4], int);
void LNSort(char[], char[], float[][4], int);
void Calculations(char[], char[], float[][4], float[], char[], int);
void AVSort(char[], char[], float[][4], float[], char[], int);


//** ******* **
void GetData(char*(FNs)[50], char*(LNs)[50], float(Scores)[][4], int &i) {            
	char wholename[21];
	int index=0, k=0;

	ifstream text("data5.txt", ios::in);
	if(text.is_open())
		while(i<50 && text.getline(wholename, 20))
		{
			int namelength=strlen(wholename);    

			FNs[i]=new char[20]; 
			strcpy(FNs[i], "");
			int j=namelength-1;
			while(!isspace(wholename[j]) && j>=0)
				j--;
			//for later
			index=j;
			strncat(FNs[i], wholename, index);

			LNs[i] = new char[20];
			index++;
			for(k=0; k!=namelength-index; k++)
				LNs[i][k]=wholename[index++];

			text>>Scores[i][0];
			text>>Scores[i][1];
			text>>Scores[i][2];				
			text>>Scores[i][3];
			i++;
		}
	text.close();
}

//** ******* **
void LNSort(char*(FNs)[], char*(LNs)[], float(Scores)[][4], int nRecords) {
    for(int k=0; k<(nRecords-1); k++)
	{
	   int min=k;
	   for(int j=k+1; j<nRecords; j++)
            if(LNs[j]<LNs[min])
               min = j;

			   swap(LNs[k], LNs[min]);        
			   swap(FNs[k], FNs[min]);
			   swap(Scores[k][0], Scores[min][0]);
			   swap(Scores[k][1], Scores[min][1]);
			   swap(Scores[k][2], Scores[min][2]);
			   swap(Scores[k][3], Scores[min][3]);
    }

	ofstream output("output.txt", ios::out | ios::app);
	if(output.is_open())
	{
		//output
	}
}

void main(void) {
	char *FNs[50];
	char *LNs[50];
	float Scores[50][4];
	int i=0;

	GetData(FNs, LNs, Scores, i);
	int nRecords=(i-1);

	LNSort(FNs, LNs, Scores, nRecords);
	}
}

solved. thanks

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.