Difference between revisions of "Microsoft KB Archive/170199"

From BetaArchive Wiki
m (Text replacement - ">" to ">")
m (Text replacement - """ to """)
 
Line 107: Line 107:
 
=== Description ===
 
=== Description ===
  
This sample demonstrates the use of STL slice array to change "Hello World!" string to "Hello There." string.
+
This sample demonstrates the use of STL slice array to change "Hello World!" string to "Hello There." string.
 
=== Sample Code ===
 
=== Sample Code ===
  
Line 135: Line 135:
 
     void main()
 
     void main()
 
     {
 
     {
       // Initialize string 1 to contain "Hello World!",
+
       // Initialize string 1 to contain "Hello World!",
       // and string 2 to contain "There".
+
       // and string 2 to contain "There".
       valarray<char> string1(&quot;Hello World!&quot;, 12);
+
       valarray<char> string1("Hello World!", 12);
       valarray<char> string2(&quot;There.&quot;, 6);
+
       valarray<char> string2("There.", 6);
  
 
       // Display both strings.
 
       // Display both strings.
       cout << &quot;String 1 = &quot;;
+
       cout << "String 1 = ";
 
       int strlen;
 
       int strlen;
 
     #if _MSC_VER == 1020  // version 4.2 - call length
 
     #if _MSC_VER == 1020  // version 4.2 - call length
Line 150: Line 150:
 
       for (int i = 0; i < strlen; i++)
 
       for (int i = 0; i < strlen; i++)
 
           cout << string1[i];
 
           cout << string1[i];
       cout << &quot;\r\n&quot;;
+
       cout << "\r\n";
  
       cout << &quot;String 2 = &quot;;
+
       cout << "String 2 = ";
 
     #if _MSC_VER == 1020  // version 4.2 - call length
 
     #if _MSC_VER == 1020  // version 4.2 - call length
 
       strlen = string2.length();
 
       strlen = string2.length();
Line 160: Line 160:
 
       for (i = 0; i < strlen; i++)
 
       for (i = 0; i < strlen; i++)
 
           cout << string2[i];
 
           cout << string2[i];
       cout << &quot;\r\n\r\n&quot;;
+
       cout << "\r\n\r\n";
  
 
       // slice(6, 6, 1) creates a slice array of
 
       // slice(6, 6, 1) creates a slice array of
 
       //{6, 7, 8, 9, 10, 11} which is used to select
 
       //{6, 7, 8, 9, 10, 11} which is used to select
       // the elements of string1. &quot;World!&quot; it is. In
+
       // the elements of string1. "World!" it is. In
 
       // turn, these elements are replaced by the corresponding
 
       // turn, these elements are replaced by the corresponding
 
       // element in string2.
 
       // element in string2.
Line 172: Line 172:
 
       // Display starting index, length,
 
       // Display starting index, length,
 
       // and stride of the slice object.
 
       // and stride of the slice object.
       cout << &quot;slice - starting index = &quot; << slice_obj.start();
+
       cout << "slice - starting index = " << slice_obj.start();
             cout << &quot;\r\n&quot;;
+
             cout << "\r\n";
 
     #if _MSC_VER == 1020  // version 4.2 - call length
 
     #if _MSC_VER == 1020  // version 4.2 - call length
       cout << &quot;slice - length = &quot; << slice_obj.length() << &quot;\r\n&quot;;
+
       cout << "slice - length = " << slice_obj.length() << "\r\n";
 
     #else  // later than 4.2 - call size
 
     #else  // later than 4.2 - call size
       cout << &quot;slice - length = &quot; << slice_obj.size() << &quot;\r\n&quot;;
+
       cout << "slice - length = " << slice_obj.size() << "\r\n";
 
     #endif
 
     #endif
       cout << &quot;slice - stride = &quot; << slice_obj.stride() << &quot;\r\n&quot;;
+
       cout << "slice - stride = " << slice_obj.stride() << "\r\n";
       cout << &quot;\r\n&quot;;
+
       cout << "\r\n";
  
 
       // Now print out the result.
 
       // Now print out the result.
       cout << &quot;After the change, String 1 = &quot;;
+
       cout << "After the change, String 1 = ";
 
     #if _MSC_VER == 1020  // version 4.2 - call length
 
     #if _MSC_VER == 1020  // version 4.2 - call length
 
       strlen = string1.length();
 
       strlen = string1.length();
Line 191: Line 191:
 
       for(i = 0; i < strlen; i++)
 
       for(i = 0; i < strlen; i++)
 
           cout << string1[i];
 
           cout << string1[i];
       cout << &quot;\r\n&quot;;
+
       cout << "\r\n";
 
     }
 
     }
 
                 </pre>
 
                 </pre>

Latest revision as of 11:04, 21 July 2020

Article ID: 170199

Article Last Modified on 12/2/2003



APPLIES TO

  • The Standard C++ Library, when used with:
    • Microsoft Visual C++ 4.2 Enterprise Edition
    • Microsoft Visual C++ 4.2 Professional Edition
    • Microsoft Visual C++ 5.0 Enterprise Edition
    • Microsoft Visual C++ 5.0 Professional Edition
    • Microsoft Visual C++ 6.0 Enterprise Edition
    • Microsoft Visual C++ 6.0 Professional Edition
    • Microsoft Visual C++ 6.0 Standard Edition



This article was previously published under Q170199

SUMMARY

The sample code below illustrates how to use the slice::length, slice::start, and slice::stride STL functions in Visual C++.

Note that there are some differences in the implementation of the Standard C++ Library components in Visual C++ version 4.2 versus later revisions. The relevant sections of code below compile conditionally based upon the value of _MSC_VER.

MORE INFORMATION

Required Header

   <valarray>
                

Prototype

   class slice
   {
   public:

      slice();
      slice(size_t st, size_t len, size_t str);
      size_t start() const;

   #if _MSC_VER == 1020 // version 4.2 - declare length()

      size_t length() const;

   #else // later than version 4.2, length changed to size()

      size_t size() const;

   #endif

      size_t stride() const;

   };
                

NOTE: The class/parameter names in the prototype may not match the version in the header file. Some have been modified to improve readability.

Description

This sample demonstrates the use of STL slice array to change "Hello World!" string to "Hello There." string.

Sample Code

    ////////////////////////////////////////////////////////////////////// 
    // 
    // Compile options needed: None
    // 
    // <filename> :  main.cpp
    // 
    // Functions:
    // 
    //  slice::length, slice::start, slice::stride
    // 
    // Written by Yeong-Kah Tam
    // of Microsoft Product Support Services,
    // Copyright (c) 1996 Microsoft Corporation. All rights reserved.
    ////////////////////////////////////////////////////////////////////// 

    #include <iostream>             // for i/o functions
    #include <valarray>             // for valarray
    using namespace std;

   #if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
   #endif

    void main()
    {
       // Initialize string 1 to contain "Hello World!",
       // and string 2 to contain "There".
       valarray<char> string1("Hello World!", 12);
       valarray<char> string2("There.", 6);

       // Display both strings.
       cout << "String 1 = ";
       int strlen;
    #if _MSC_VER == 1020   // version 4.2 - call length
       strlen = string1.length();
    #else   // later than 4.2 - call size
       strlen = string1.size();
    #endif
       for (int i = 0; i < strlen; i++)
          cout << string1[i];
       cout << "\r\n";

       cout << "String 2 = ";
    #if _MSC_VER == 1020   // version 4.2 - call length
       strlen = string2.length();
    #else   // later than 4.2 - call size
       strlen = string2.size();
    #endif
       for (i = 0; i < strlen; i++)
          cout << string2[i];
       cout << "\r\n\r\n";

       // slice(6, 6, 1) creates a slice array of
       //{6, 7, 8, 9, 10, 11} which is used to select
       // the elements of string1. "World!" it is. In
       // turn, these elements are replaced by the corresponding
       // element in string2.
       slice slice_obj(6, 6, 1);
       string1[slice_obj] = string2;

       // Display starting index, length,
       // and stride of the slice object.
       cout << "slice - starting index = " << slice_obj.start();
            cout << "\r\n";
    #if _MSC_VER == 1020   // version 4.2 - call length
       cout << "slice - length = " << slice_obj.length() << "\r\n";
    #else   // later than 4.2 - call size
       cout << "slice - length = " << slice_obj.size() << "\r\n";
    #endif
       cout << "slice - stride = " << slice_obj.stride() << "\r\n";
       cout << "\r\n";

       // Now print out the result.
       cout << "After the change, String 1 = ";
    #if _MSC_VER == 1020   // version 4.2 - call length
       strlen = string1.length();
    #else   // later than 4.2 - call size
       strlen = string1.size();
    #endif
       for(i = 0; i < strlen; i++)
          cout << string1[i];
       cout << "\r\n";
    }
                

Program Output

   String 1 = Hello World!
   String 2 = There.

   slice - starting index = 6
   slice - length = 6
   slice - stride = 1
                

After the change, String 1 = Hello There.

REFERENCES

Visual C++ Books On Line: Visual C++ Books:C/C++:Standard C++ Library Reference.


Additional query words: STL STLSample length start stride (c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Yeong-Kah Tam,

Keywords: kbhowto KB170199