[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] Dynamisk alloceret arrays



I C++ standarden findes en streng klasse string, som
tilbyder dynamisk memory management, og det på en måde
som tillader elegant kobling til oprindelig streng
manipulering ved null-terminerede karakterstrenge.

Jeg har lavet dig et lille eksempel:

-----------------
// $Header: $

// File     : stl_string.cpp
// Signature: jk
// Creation : 19990808

// Reference: [1] Herbert Schildt "C++: The Complete Reference, Third
Edition"

// Compilation: c++ -o stl_string stl_string.cpp

#include <iostream>
#include <string>
using namespace std;

char str[80];
char *f1()
{
  strcpy(str,"... C-string added in f1 ...");
  return str;
} // f1

void f2(const char *pstr)
{
  cout << "f2 printing C-string: '" << pstr << "'" << endl;
}

void t1()
{ // Test 1, use string in connection with
  //         C-string (which uses null-termination)
  // Ref [1], p 688
  // Output:
  // ----
  //   Initial size: 0
  //   Now size is 16  and str1= 'Initial contents'
  //   f2 printing C-string: 'Initial contents... C-string added in f1 ...'
  //   size = 44
  // ----
  string str1;

  cout << "Initial size: " << str1.size() << endl;

  str1 = "Initial contents";
  cout << "Now size is " << str1.size()
       << "  and str1= '" << str1 << "'" << endl;

  // Show how to add C-string dynamically to string
  str1 += f1();

  // Show how to use string member returning C-string
  f2(str1.c_str());
  cout << "size = " << str1.size() << endl;
} // Test 1

void main(void)
{
  t1();
}
-----------------

"Thomas M." wrote:

> Hejsa
>
>  Er der en mulighed for at lave dynamisk allocerede arrays, således at de
> kan vokse, når der bliver brug for det?
>  Det er til et projekt, hvor jeg bygger en query op til mysql, og jeg ved
> ikke fra starten hvor stor denne query streng vil blive. Kunne
> selvfølgelig bare allocere 2Kb (eller mere) til strengen, men jeg føler
> at det er den forkerte metode til det...
>
>  Nogen ideer??
>
> Thomas Mørch
> licensed ham, with callsign OZ2AFO. License category: CEPT-1
>
> If God had intended man to smoke He would have set him on fire.



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:07 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *