VoyForums
[ Show ]
Support VoyForums
[ Shrink ]
VoyForums Announcement: Programming and providing support for this service has been a labor of love since 1997. We are one of the few services online who values our users' privacy, and have never sold your information. We have even fought hard to defend your privacy in legal cases; however, we've done it with almost no financial support -- paying out of pocket to continue providing the service. Due to the issues imposed on us by advertisers, we also stopped hosting most ads on the forums many years ago. We hope you appreciate our efforts.

Show your support by donating any amount. (Note: We are still technically a for-profit company, so your contribution is not tax-deductible.) PayPal Acct: Feedback:

Donate to VoyForums (PayPal):

Login ] [ Contact Forum Admin ] [ Main index ] [ Post a new message ] [ Search | Check update time | Archives: 1 ]
Subject: Forritunarverkefni 2


Author:
Ymir
[ Next Thread | Previous Thread | Next Message | Previous Message ]
Date Posted: 00:00:33 03/17/04 Wed

Mig langaði að benda fólki á að í stað þess eyða púðri í að búa til ofsafenginn kóða til að aðskilja orð (sbr. C aðferðir á borð við strtok(), eða strchr() ásamt isspace()) er til afar einföld leið til að lesa inn strengi með string klasanum í C++:

string s;
cin >> s;

mun lesa inn næsta orð úr in-straumnum í s. Ef t.d. inntak er "Orð bla halló!" í eftirfarandi forritsbút:

string s;
cin >> s; cout << "'" << s << "', ";
cin >> s; cout << "'" << s << "', ";
cin >> s; cout << "'" << s << "'.";

mun prentast út 'Orð', 'bla', 'halló!'. Að sjálfsögðu má nota aðra strauma í þessum tilgangi, t.a.m. væri hentugt að nota:

string s;
ifstream inp("bok.txt");
while (inp >> s)
{
cout << "Las orðið '" << s << "'!\n";
}

sem mun hætta þegar öll orð úr skránni 'bok.txt' hafa verið lesin.

Auk þess hafa borist spurningar um hvernig bera megi saman tvo strengi til að raða þeim í röð. String klasinn býður einfaldlega upp á aðgerðirnar '<', '>', og '==' fyrir samanburð. Dæmi:

string s1 = "Lambakaka", s2 = "Lambabaka";
if (s1 > s2) cout << "Whee!" << endl;

mun skila "Whee!" þar sem fyrri strengurinn er aftar en hinn seinni í stafrófinu m.t.t. ASCI-gilda.

Vona að þetta nýtist einhverjum!

Kveðja,
Ýmir

[ Next Thread | Previous Thread | Next Message | Previous Message ]


Post a message:
This forum requires an account to post.
[ Create Account ]
[ Login ]
[ Contact Forum Admin ]


Forum timezone: GMT+0
VF Version: 3.00b, ConfDB:
Before posting please read our privacy policy.
VoyForums(tm) is a Free Service from Voyager Info-Systems.
Copyright © 1998-2019 Voyager Info-Systems. All Rights Reserved.