Читаем Thinking In C++. Volume 2: Practical Programming полностью

There are no functions in the string class to change the case of a string, but you can easily create these functions using the Standard C library functions toupper( ) and tolower( ), which change the case of one character at a time. The following example illustrates a case-insensitive search:.

//: C03:Find.cpp

//{L} ../TestSuite/Test

#include

#include

#include

#include "../TestSuite/Test.h"

using namespace std;

// Make an uppercase copy of s

string upperCase(const string& s) {

  string upper(s);

  for(size_t i = 0; i < s.length(); ++i)

    upper[i] = toupper(upper[i]);

  return upper;

}

// Make a lowercase copy of s

string lowerCase(const string& s) {

  string lower(s);

  for(size_t i = 0; i < s.length(); ++i)

    lower[i] = tolower(lower[i]);

  return lower;

}

class FindTest : public TestSuite::Test {

  string chooseOne;

public:

  FindTest() : chooseOne("Eenie, Meenie, Miney, Mo") {}

  void testUpper() {

    string upper = upperCase(chooseOne);

    const string LOWER = "abcdefghijklmnopqrstuvwxyz";

    test_(upper.find_first_of(LOWER) == string::npos);

  }

  void testLower() {

    string lower = lowerCase(chooseOne);

    const string UPPER = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    test_(lower.find_first_of(UPPER) == string::npos);

  }

  void testSearch() {

    // Case sensitive search

    size_t i = chooseOne.find("een");

    test_(i == 8);

    // Search lowercase:

    string test = lowerCase(chooseOne);

    i = test.find("een");

    test_(i == 0);

    i = test.find("een", ++i);

    test_(i == 8);

    i = test.find("een", ++i);

    test_(i == string::npos);

    // Search uppercase:

    test = upperCase(chooseOne);

    i = test.find("EEN");

    test_(i == 0);

    i = test.find("EEN", ++i);

    test_(i == 8);

    i = test.find("EEN", ++i);

    test_(i == string::npos);

  }

  void run() {

    testUpper();

    testLower();

    testSearch();

  }

};

int main() {

  FindTest t;

  t.run();

  return t.report();

} ///:~

Both the upperCase( ) and lowerCase( ) functions follow the same form: they make a copy of the argument string and change the case. The NewFind.cpp program isn’t the best solution to the case-sensitivity problem, so we’ll revisit it when we examine string comparisons.

<p>Finding in reverse</p>

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.

//: C03:Rparse.cpp

//{L} ../TestSuite/Test

#include

#include

#include "../TestSuite/Test.h"

using namespace std;

class RparseTest : public TestSuite::Test {

  // To store the words:

  vector strings;

public:

  void parseForData() {

    // The ';' characters will be delimiters

    string s("now.;sense;make;to;going;is;This");

    // 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:

      ++current;

      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 + 1;

      // 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));

  }

  void testData() {

    // Test order them in the new order:

    test_(strings[0] == "This");

    test_(strings[1] == "is");

    test_(strings[2] == "going");

    test_(strings[3] == "to");

    test_(strings[4] == "make");

    test_(strings[5] == "sense");

    test_(strings[6] == "now.");

    string sentence;

    for(int i = 0; i < strings.size() - 1; i++)

      sentence += strings[i] += " ";

    // Manually put last word in to avoid an extra space

    sentence += strings[strings.size() - 1];

    test_(sentence == "This is going to make sense now.");

  }

  void run() {

    parseForData();

    testData();

  }

};

int main() {

  RparseTest t;

  t.run();

  return t.report();

} ///:~

The string member function rfind( ) backs through the string looking for tokens and reporting the array index of matching characters or string::npos if it is unsuccessful.

<p>Finding first/last of a set of characters</p>

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 from both ends of a string. Notice that it doesn’t touch the original string, but instead returns a new string:.

//: C03:Trim.h

#ifndef TRIM_H

#define TRIM_H

#include

// General tool to strip spaces from both ends:

inline std::string trim(const std::string& s) {

  if(s.length() == 0)

    return s;

  int beg = s.find_first_not_of(" \a\b\f\n\r\t\v");

  int end = s.find_last_not_of(" \a\b\f\n\r\t\v");

  if(beg == std::string::npos) // No non-spaces

    return "";

  return std::string(s, beg, end - beg + 1);

}

#endif // TRIM_H ///:~

Перейти на страницу:

Похожие книги

1С: Бухгалтерия 8 с нуля
1С: Бухгалтерия 8 с нуля

Книга содержит полное описание приемов и методов работы с программой 1С:Бухгалтерия 8. Рассматривается автоматизация всех основных участков бухгалтерии: учет наличных и безналичных денежных средств, основных средств и НМА, прихода и расхода товарно-материальных ценностей, зарплаты, производства. Описано, как вводить исходные данные, заполнять справочники и каталоги, работать с первичными документами, проводить их по учету, формировать разнообразные отчеты, выводить данные на печать, настраивать программу и использовать ее сервисные функции. Каждый урок содержит подробное описание рассматриваемой темы с детальным разбором и иллюстрированием всех этапов.Для широкого круга пользователей.

Алексей Анатольевич Гладкий

Программирование, программы, базы данных / Программное обеспечение / Бухучет и аудит / Финансы и бизнес / Книги по IT / Словари и Энциклопедии
1С: Управление торговлей 8.2
1С: Управление торговлей 8.2

Современные торговые предприятия предлагают своим клиентам широчайший ассортимент товаров, который исчисляется тысячами и десятками тысяч наименований. Причем многие позиции могут реализовываться на разных условиях: предоплата, отсрочка платежи, скидка, наценка, объем партии, и т.д. Клиенты зачастую делятся на категории – VIP-клиент, обычный клиент, постоянный клиент, мелкооптовый клиент, и т.д. Товарные позиции могут комплектоваться и разукомплектовываться, многие товары подлежат обязательной сертификации и гигиеническим исследованиям, некондиционные позиции необходимо списывать, на складах периодически должна проводиться инвентаризация, каждая компания должна иметь свою маркетинговую политику и т.д., вообщем – современное торговое предприятие представляет живой организм, находящийся в постоянном движении.Очевидно, что вся эта кипучая деятельность требует автоматизации. Для решения этой задачи существуют специальные программные средства, и в этой книге мы познакомим вам с самым популярным продуктом, предназначенным для автоматизации деятельности торгового предприятия – «1С Управление торговлей», которое реализовано на новейшей технологической платформе версии 1С 8.2.

Алексей Анатольевич Гладкий

Финансы / Программирование, программы, базы данных