Sujet : Re: Isn't that beauty ? (no it's not)
De : Bonita.Montero (at) *nospam* gmail.com (Bonita Montero)
Groupes : comp.lang.c comp.lang.c++Date : 12. Mar 2026, 16:10:41
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <10oul1g$241b4$1@raubtier-asyl.eternal-september.org>
References : 1 2 3 4
User-Agent : Mozilla Thunderbird
Am 12.03.2026 um 15:43 schrieb DFS:
On 3/12/2026 10:13 AM, Bonita Montero wrote:
Here, do that in C:
>
static optional<size_t> parse( const char *str )
{
size_t ret;
if( from_chars_result fcr = from_chars( str, str + strlen( str ), ret ); (bool)fcr.ec || *fcr.ptr )
return nullopt;
return ret;
}
Explain in detail what it does.
If that isn't self-explanatory stick with C.
I've got a task for you: Do the same in C:
#include <iostream>
#include <regex>
#include <fstream>
#include <vector>
#include <algorithm>
#include <vector>
using namespace std;
int main( int argc, char **argv )
{
if( argc < 2 )
return EXIT_FAILURE;
ifstream ifs( argv[1] );
static regex rxNameTel( "^\s*\"([^\"]*)\"\s*\"([^\"]*)\"\s*$" );
struct name_tel { string name, tel; };
vector<name_tel> phoneList;
while( !ifs.eof() )
{
string line;
getline( ifs, line );
match_results<string::const_iterator> sm;
if( regex_match( line, sm, rxNameTel ) )
phoneList.emplace_back( string( sm[1].first, sm[1].second ), string( sm[2].first, sm[2].second ) );
}
sort( phoneList.begin(), phoneList.end(),
[]( const name_tel &left, const name_tel &right ) { return left.name < right.name; } );
for( name_tel &phone : phoneList )
cout << "\"" << phone.name << "\"\t\"" << phone.tel << "\"" << endl;
}
1. Read a file and parse it with ne mentioned regex-pattern.
2. Split both parts of every line in two strings.
3. Sort the "vector" according to the first string.
4. Print it.
I guess you don't manage to do that with less than five times the work.
Every external lib allowed.
Haut de la page
Les messages affichés proviennent d'usenet.
NewsPortal