Click here to Skip to main content
15,888,816 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Sorting a vector of non-primitive data types (structs) Pin
Christian Graus27-Mar-07 3:12
protectorChristian Graus27-Mar-07 3:12 
AnswerRe: Sorting a vector of non-primitive data types (structs) Pin
Stephen Hewitt27-Mar-07 4:05
Stephen Hewitt27-Mar-07 4:05 
GeneralRe: Sorting a vector of non-primitive data types (structs) Pin
all_in_flames27-Mar-07 5:16
professionalall_in_flames27-Mar-07 5:16 
GeneralRe: Sorting a vector of non-primitive data types (structs) Pin
Christian Graus27-Mar-07 6:02
protectorChristian Graus27-Mar-07 6:02 
GeneralRe: Sorting a vector of non-primitive data types (structs) Pin
Stephen Hewitt27-Mar-07 13:33
Stephen Hewitt27-Mar-07 13:33 
GeneralRe: Sorting a vector of non-primitive data types (structs) Pin
Stephen Hewitt27-Mar-07 13:33
Stephen Hewitt27-Mar-07 13:33 
QuestionRe: Sorting a vector of non-primitive data types (structs) Pin
all_in_flames27-Mar-07 12:12
professionalall_in_flames27-Mar-07 12:12 
AnswerRe: Sorting a vector of non-primitive data types (structs) Pin
Stephen Hewitt27-Mar-07 13:47
Stephen Hewitt27-Mar-07 13:47 
There are multiple ways, here's one:
// CommandLine.cpp : Defines the entry point for the console application.
//
 
#include "StdAfx.h"
#include <iostream>
#include <string>
#include <sstream> // For 'istringstream'.
 
int main()
{
	using namespace std;
 
	string s = "1234";
	int num;
	istringstream ss(s);
	ss >> num;
	if (ss)
	{
		cout << "Number is " << num << endl;
	}
 
	return 0;
}


And another:
// CommandLine.cpp : Defines the entry point for the console application.
//
 
#include "StdAfx.h"
#include <iostream>
#include <string>
#include <cstdlib>
 
int main()
{
	using namespace std;
 
	string s = "1234";
	int num = atoi(s.c_str());
	cout << "Number is " << num << endl;
 
	return 0;
}


The second method is easier but error handling is more difficult as if no valid conversion could be performed, a zero value is returned. Also, in the first version simply changing the type of the num variable will make the code work different types but the second version requires a distinct function for each type: atoi only works for ints.


Steve

GeneralRe: Sorting a vector of non-primitive data types (structs) Pin
all_in_flames27-Mar-07 13:54
professionalall_in_flames27-Mar-07 13:54 
GeneralRe: Sorting a vector of non-primitive data types (structs) Pin
Stephen Hewitt27-Mar-07 13:57
Stephen Hewitt27-Mar-07 13:57 
AnswerRe: Sorting a vector of non-primitive data types (structs) Pin
Michael Dunn27-Mar-07 20:36
sitebuilderMichael Dunn27-Mar-07 20:36 
GeneralRe: Sorting a vector of non-primitive data types (structs) Pin
all_in_flames28-Mar-07 2:16
professionalall_in_flames28-Mar-07 2:16 
Questionlparam and wparam Pin
Try27-Mar-07 2:59
Try27-Mar-07 2:59 
AnswerRe: lparam and wparam Pin
David Crow27-Mar-07 3:03
David Crow27-Mar-07 3:03 
QuestionRe: lparam and wparam Pin
Try27-Mar-07 3:12
Try27-Mar-07 3:12 
AnswerRe: lparam and wparam Pin
David Crow27-Mar-07 3:18
David Crow27-Mar-07 3:18 
GeneralRe: lparam and wparam Pin
Michael Dunn27-Mar-07 20:40
sitebuilderMichael Dunn27-Mar-07 20:40 
AnswerRe: lparam and wparam Pin
prasad_som27-Mar-07 4:00
prasad_som27-Mar-07 4:00 
AnswerRe: lparam and wparam Pin
Christian Graus27-Mar-07 3:04
protectorChristian Graus27-Mar-07 3:04 
GeneralRe: lparam and wparam Pin
Mark Salsbery27-Mar-07 7:10
Mark Salsbery27-Mar-07 7:10 
Questionlearning multithreading Pin
l_d27-Mar-07 2:44
l_d27-Mar-07 2:44 
AnswerRe: learning multithreading Pin
David Crow27-Mar-07 2:45
David Crow27-Mar-07 2:45 
AnswerRe: learning multithreading Pin
Roger Stoltz27-Mar-07 2:46
Roger Stoltz27-Mar-07 2:46 
AnswerRe: learning multithreading Pin
Try27-Mar-07 2:46
Try27-Mar-07 2:46 
QuestionI ve created an MFC(exe) project and its ana Dialog based one. how to hide the dialog during runtime? Pin
Malini Nair27-Mar-07 2:11
Malini Nair27-Mar-07 2:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.