Strona startowa Ludzie pragną czasami się rozstawać, żeby móc tęsknić, czekać i cieszyć się z powrotem.It has been shown that large pharmaceutical companies set up groups whose job it is to ensure integrity, quality, and safety in the company's output...Output strstreamsOutput strstreams also allow you to provide your own storage; in this case it’s the place in memory the bytes are formatted into...output; unput(c) sends the character c back in the text to be read by the next input()...The last two lines of the error output are actually a stack backtrace...1876 * 1 1 FOR 03 04 32...Gdyby Boemund otrzymał list patriarchy, losy jego ułożyłyby się znacznie pomyślniej...najgłębszy dziś znawca ich kultury, Jakob Burckhardt z Bazylei, ten odich wnętrzu...wie jak najwcześniej zdejmować z protezy, dając okazję do jej przegląuproszczone...osób lub poszczególnej osoby z powodu jej przynależności narodowej,etnicznej, rasowej, politycznej, wyznaniowej lub z powodu jejbezwyznaniowości, podlega...Edwin Bannister, sekretarz redakcji "Boston Globe", wymeldowal sie z zajazdu i dopilnowal zaladunku bagazy na tyl swego range rove-ra...
 

Ludzie pragną czasami się rozstawać, żeby móc tęsknić, czekać i cieszyć się z powrotem.


NewFind.cpp isn’t the best solution to the case sensitivity problem, so we’ll revisit it when we examine string comparisons.
Chapter 14: Templates & Container Classes
522
Finding in reverse
Sometimes it’s necessary to search through a string from end to beginning, if you need to find the data in “last in / first out “ order. The string member function rfind( ) handles this job.
//: C17:Rparse.cpp
// Reverse the order of words in a string
#include <string>
#include <iostream>
#include <vector>
using namespace std;
int main() {
// The ';' characters will be delimiters
string s("now.;sense;make;to;going;is;This");
cout << s << endl;
// To store the words:
vector<string> strings;
// The last element of the string:
int last = s.size();
// The beginning of the current word:
int current = s.rfind(';');
// Walk backward through the string:
while(current != string::npos){
// Push each word into the vector.
// Current is incremented before copying to
// avoid copying the delimiter.
strings.push_back(
s.substr(++current,last - current));
// Back over the delimiter we just found,
// and set last to the end of the next word
current -= 2;
last = current;
// Find the next delimiter
current = s.rfind(';', current);
}
// Pick up the first word - it's not
// preceded by a delimiter
strings.push_back(s.substr(0, last - current));
// Print them in the new order:
for(int j = 0; j < strings.size(); j++)
cout << strings[j] << " ";
} ///:~
Chapter 14: Templates & Container Classes
523
Here’s how the output from Rparse.cpp looks:
now.;sense;make;to;going;is;This
This is going to make sense now.
rfind( ) backs through the string looking for tokens, reporting the array index of matching characters or string::npos if it is unsuccessful.
Finding first/last of a set
The find_first_of( ) and find_last_of( ) member functions can be conveniently put to work to create a little utility that will strip whitespace characters off of both ends of a string. Notice it doesn’t touch the original string, but instead returns a new string:
//: C17:trim.h
#ifndef TRIM_H
#define TRIM_H
#include <string>
// General tool to strip spaces from both ends:
inline std::string trim(const std::string& s) {
if(s.length() == 0)
return s;
int b = s.find_first_not_of(" \t");
int e = s.find_last_not_of(" \t");
if(b == -1) // No non-spaces
return "";
return std::string(s, b, e - b + 1);
}
#endif // TRIM_H ///:~
The first test checks for an empty string; in that case no tests are made and a copy is returned.
Notice that once the end points are found, the string constructor is used to build a new string from the old one, giving the starting count and the length. This form also utilizes the “return value optimization” (see the index for more details).
Testing such a general-purpose tool needs to be thorough:
//: C17:TrimTest.cpp
#include "trim.h"
#include <iostream>
using namespace std;
string s[] = {
" \t abcdefghijklmnop \t ",
"abcdefghijklmnop \t ",
" \t abcdefghijklmnop",
Chapter 14: Templates & Container Classes
524
"a", "ab", "abc", "a b c",
" \t a b c \t ", " \t a \t b \t c \t ",
"", // Must also test the empty string
};
void test(string s) {
cout << "[" << trim(s) << "]" << endl;
}
int main() {
for(int i = 0; i < sizeof s / sizeof *s; i++)
test(s[i]);
} ///:~
In the array of string s, you can see that the character arrays are automatically converted to string objects. This array provides cases to check the removal of spaces and tabs from both ends, as well as ensuring that spaces and tabs do not get removed from the middle of a string.
Removing characters from strings
My word processor/page layout program (Microsoft Word) will save a document in HTML,
but it doesn’t recognize that the code listings in this book should be tagged with the HTML