Microsoft KB Archive/170198: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - """ to """)
 
(2 intermediate revisions by the same user not shown)
Line 77: Line 77:
=== Required Header ===
=== Required Header ===


<pre class="codesample">  &lt;valarray&gt;
<pre class="codesample">  <valarray>
                 </pre>
                 </pre>
=== Prototype ===
=== Prototype ===
Line 86: Line 86:


       gslice();
       gslice();
       gslice(size_t st, const valarray&lt;size_t&gt; len,
       gslice(size_t st, const valarray<size_t> len,
                   const valarray&lt;size_t&gt; str);
                   const valarray<size_t> str);
       size_t start() const;
       size_t start() const;


   #if _MSC_VER == 1020 // if VC++ 4.2, define length()
   #if _MSC_VER == 1020 // if VC++ 4.2, define length()


       const valarray&lt;size_t&gt; length() const;
       const valarray<size_t> length() const;


   #else // if later than VC++ 4.2, define size() instead
   #else // if later than VC++ 4.2, define size() instead


       const valarray&lt;size_t&gt; size() const;
       const valarray<size_t> size() const;


   #endif
   #endif


       const valarray&lt;size_t&gt; stride() const;
       const valarray<size_t> stride() const;


   };
   };
Line 107: Line 107:
=== Description ===
=== Description ===


This sample demostrates the use of STL gslice array to change &quot;Hello World!&quot; string to &quot;Hello There.&quot; string.
This sample demostrates the use of STL gslice array to change "Hello World!" string to "Hello There." string.
=== Sample Code ===
=== Sample Code ===


Line 114: Line 114:
     // Compile options needed: None
     // Compile options needed: None
     //  
     //  
     // &lt;filename&gt; :  main.cpp
     // <filename> :  main.cpp
     //  
     //  
     // Functions:
     // Functions:
Line 125: Line 125:
     //////////////////////////////////////////////////////////////////////  
     //////////////////////////////////////////////////////////////////////  


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




     #if _MSC_VER &gt; 1020 // if later than VC++ 4.2
     #if _MSC_VER > 1020 // if later than VC++ 4.2
     using namespace std;  // the std C++ libs are in namespace std
     using namespace std;  // the std C++ libs are in namespace std
     #endif
     #endif
Line 136: Line 136:
     void main()
     void main()
     {
     {
       // Initialize string 1 to contain &quot;Hello World!&quot;, and string 2 to
       // Initialize string 1 to contain "Hello World!", and string 2 to
       // contain &quot;There&quot;.
       // contain "There".
       valarray&lt;char&gt; string1(&quot;Hello World!&quot;, 12);
       valarray<char> string1("Hello World!", 12);
       valarray&lt;char&gt; string2(&quot;There.&quot;, 6);
       valarray<char> string2("There.", 6);


       // Display both strings.
       // Display both strings.
       cout &lt;&lt; &quot;String 1 = &quot;;
       cout << "String 1 = ";
       int strlen;
       int strlen;
     #if _MSC_VER == 1020  // VC++ 4.2 - call length()
     #if _MSC_VER == 1020  // VC++ 4.2 - call length()
Line 149: Line 149:
       strlen = string1.size();
       strlen = string1.size();
     #endif
     #endif
       for (int i = 0; i &lt; strlen; i++)
       for (int i = 0; i < strlen; i++)
           cout &lt;&lt; string1[i];
           cout << string1[i];
       cout &lt;&lt; &quot;\r\n&quot;;
       cout << "\r\n";


       cout &lt;&lt; &quot;String 2 = &quot;;
       cout << "String 2 = ";
     #if _MSC_VER == 1020  // VC++ 4.2 - call length()
     #if _MSC_VER == 1020  // VC++ 4.2 - call length()
       strlen = string2.length();
       strlen = string2.length();
Line 159: Line 159:
       strlen = string2.size();
       strlen = string2.size();
     #endif
     #endif
       for (i = 0; i &lt; strlen; i++)
       for (i = 0; i < strlen; i++)
           cout &lt;&lt; string2[i];
           cout << string2[i];
       cout &lt;&lt; &quot;\r\n\r\n&quot;;
       cout << "\r\n\r\n";


       // Create 2 vectors for use by gslice().
       // Create 2 vectors for use by gslice().
       const size_t lv[] = {2, 3};  // number of initial indexes
       const size_t lv[] = {2, 3};  // number of initial indexes
       const size_t dv[] = {3, 1};  // increment value
       const size_t dv[] = {3, 1};  // increment value
       const valarray&lt;size_t&gt; len(lv, 2), stride(dv, 2);
       const valarray<size_t> len(lv, 2), stride(dv, 2);


       // gslice(6, len, stride) creates a gslice
       // gslice(6, len, stride) creates a gslice
             // array of {6, 7, 8, 9, 10, 11} which is used to select the
             // array of {6, 7, 8, 9, 10, 11} which is used to select the
             // elements of string1.  &quot;World!&quot; it is. In turn, these
             // elements of string1.  "World!" it is. In turn, these
             // elements are replaced by the corresponding element
             // elements are replaced by the corresponding element
       // in string2.
       // in string2.
Line 178: Line 178:
       // Display starting index, length vector, and stride vector of
       // Display starting index, length vector, and stride vector of
             // the gslice object.
             // the gslice object.
       cout &gt;&gt; &quot;gslice - starting index = &quot; &gt;&gt; gslice_obj.start()
       cout >> "gslice - starting index = " >> gslice_obj.start()
               &gt;&gt; &quot;\r\n&quot;;
               >> "\r\n";


     #if _MSC_VER == 1020  // VC++ 4.2 - call length()
     #if _MSC_VER == 1020  // VC++ 4.2 - call length()
       valarray&lt;size_t&gt; gslice_len = gslice_obj.length();
       valarray<size_t> gslice_len = gslice_obj.length();
       strlen = gslice_len.length();
       strlen = gslice_len.length();
     #else  // later than VC++ 4.2 - call size()
     #else  // later than VC++ 4.2 - call size()
       valarray&lt;size_t&gt; gslice_len = gslice_obj.size();
       valarray<size_t> gslice_len = gslice_obj.size();
       strlen = gslice_len.size();
       strlen = gslice_len.size();
     #endif
     #endif
       cout &lt;&lt; &quot;gslice - length vector  = &quot;;
       cout << "gslice - length vector  = ";
       for (i = 0; i &lt; strlen; i++)
       for (i = 0; i < strlen; i++)
           cout &lt;&lt; gslice_len[i] &lt;&lt; &quot; &quot;;
           cout << gslice_len[i] << " ";
       cout &lt;&lt; &quot;\r\n&quot;;
       cout << "\r\n";


       valarray&lt;size_t&gt; gslice_stride = gslice_obj.stride();
       valarray<size_t> gslice_stride = gslice_obj.stride();
     #if _MSC_VER == 1020  // VC++ 4.2 - call length()
     #if _MSC_VER == 1020  // VC++ 4.2 - call length()
       strlen = gslice_stride.length();
       strlen = gslice_stride.length();
Line 199: Line 199:
       strlen = gslice_stride.size();
       strlen = gslice_stride.size();
     #endif
     #endif
       cout &lt;&lt; &quot;gslice - stride vector  = &quot;;
       cout << "gslice - stride vector  = ";
       for (i = 0; i &lt; strlen; i++)
       for (i = 0; i < strlen; i++)
           cout &lt;&lt; gslice_stride[i] &lt;&lt; &quot; &quot;;
           cout << gslice_stride[i] << " ";
       cout &lt;&lt; &quot;\r\n\r\n&quot;;
       cout << "\r\n\r\n";


       // Now print out the result.
       // Now print out the result.
       cout &lt;&lt; &quot;After the change, String 1 = &quot;;
       cout << "After the change, String 1 = ";
     #if _MSC_VER == 1020  // VC++ 4.2 - call length()
     #if _MSC_VER == 1020  // VC++ 4.2 - call length()
       strlen = string1.length();
       strlen = string1.length();
Line 211: Line 211:
       strlen = string1.size();
       strlen = string1.size();
     #endif
     #endif
       for(i = 0; i &lt; strlen; i++)
       for(i = 0; i < strlen; i++)
           cout &lt;&lt; string1[i];
           cout << string1[i];
       cout &lt;&lt; &quot;\r\n&quot;;
       cout << "\r\n";
     }
     }
                 </pre>
                 </pre>

Latest revision as of 11:04, 21 July 2020

Article ID: 170198

Article Last Modified on 12/10/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 Q170198

SUMMARY

The sample code below illustrates how to use the gslice::length, gslice::start, and gslice::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 gslice
   {
   public:

      gslice();
      gslice(size_t st, const valarray<size_t> len,
                  const valarray<size_t> str);
      size_t start() const;

   #if _MSC_VER == 1020 // if VC++ 4.2, define length()

      const valarray<size_t> length() const;

   #else // if later than VC++ 4.2, define size() instead

      const valarray<size_t> size() const;

   #endif

      const valarray<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 demostrates the use of STL gslice array to change "Hello World!" string to "Hello There." string.

Sample Code

    ////////////////////////////////////////////////////////////////////// 
    // 
    // Compile options needed: None
    // 
    // <filename> :  main.cpp
    // 
    // Functions:
    // 
    //  gslice::length, gslice::start, gslice::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 later than VC++ 4.2
    using namespace std;   // the std C++ libs are in namespace 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   // VC++ 4.2 - call length()
       strlen = string1.length();
    #else   // later than VC++ 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   // VC++ 4.2 - call length()
       strlen = string2.length();
    #else   // later than VC++ 4.2 - call size()
       strlen = string2.size();
    #endif
       for (i = 0; i < strlen; i++)
          cout << string2[i];
       cout << "\r\n\r\n";

       // Create 2 vectors for use by gslice().
       const size_t lv[] = {2, 3};  // number of initial indexes
       const size_t dv[] = {3, 1};  // increment value
       const valarray<size_t> len(lv, 2), stride(dv, 2);

       // gslice(6, len, stride) creates a gslice
            // 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.
       gslice gslice_obj(6, len, stride);
       string1[gslice_obj] = string2;

       // Display starting index, length vector, and stride vector of
            // the gslice object.
       cout >> "gslice - starting index = " >> gslice_obj.start()
              >> "\r\n";

    #if _MSC_VER == 1020   // VC++ 4.2 - call length()
       valarray<size_t> gslice_len = gslice_obj.length();
       strlen = gslice_len.length();
    #else   // later than VC++ 4.2 - call size()
       valarray<size_t> gslice_len = gslice_obj.size();
       strlen = gslice_len.size();
    #endif
       cout << "gslice - length vector  = ";
       for (i = 0; i < strlen; i++)
          cout << gslice_len[i] << "  ";
       cout << "\r\n";

       valarray<size_t> gslice_stride = gslice_obj.stride();
    #if _MSC_VER == 1020   // VC++ 4.2 - call length()
       strlen = gslice_stride.length();
    #else   // later than VC++ 4.2 - call size()
       strlen = gslice_stride.size();
    #endif
       cout << "gslice - stride vector  = ";
       for (i = 0; i < strlen; i++)
          cout << gslice_stride[i] << "  ";
       cout << "\r\n\r\n";

       // Now print out the result.
       cout << "After the change, String 1 = ";
    #if _MSC_VER == 1020   // VC++ 4.2 - call length()
       strlen = string1.length();
    #else   // later than VC++ 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.

   gslice - starting index = 6
   gslice - length vector  = 2  3
   gslice - stride vector  = 3  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 KB170198