Originally Posted by
laserlight
In that case, a simple method would be to use a loop:
Code:
std::string str = "hello world";
std::string str_array[15];
const std::size_t str_array_size = sizeof(str_array) / sizeof(str_array[0]);
for (std::size_t i = 0; i < str_array_size; ++i)
{
str_array[i] = str;
}
A more succinct method would be to #include <algorithm> and use std::fill(), e.g.,
Code:
std::fill(str_array, str_array + str_array_size, str);
or std::fill_n(), e.g.,
Code:
std::fill_n(str_array, str_array_size, str);
In the context of your earlier question about converting a numeric string in octal representation to a numeric string in binary representation a loop would probably be good enough.
many tanx dear laserlight . really tanx .
Originally Posted by
Elysia
I believe I already
showed you an example on how to use string streams, no?
As for .c_str():
Code:
std::string str = "10";
long x = strtol(str.c_str());
Or if you want:
Code:
std::string str = "10";
long x = boost::lexical_cast<long>(str);
The second example works to convert a given type to
any type, so long as that conversion is supported and possible. You don't need to remember 5 or more functions to do a conversion. Just one.
tanx dear alysia , no you didnt give me any examples ( i think) .
but thanks again . now its fine ...
------------------------------
now the function works just fine . thank you all ( i used all of copy functions you ve told me , to remember them :d
octal to binary convertor
Code:
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
// Octal to binary convertor
string Oct2Bin(int);
int main(int argc, char *argv[])
{
int b,i=0,j=0;
char memory[5][10];
char * str2,*ptr;
string str;
cout<<"enter a number ";
cin>>b;
str=Oct2Bin(b);
ptr = new char [str.size()+1];
strcpy (ptr, str.c_str());
cout<<ptr<<endl;
if (str=="no")
{
cout<<"not an octal number\n";
}
cout<<str<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
string Oct2Bin(int n)
{
int a[6],i=0,t=0,num;
string str,str1,str2,s;
char buffer[9];
char * ptr, * not_octal;
string str_array[9];
const size_t str_array_size = sizeof(str_array) / sizeof(str_array[0]);
while(n!=0)
{
a[i]=n%10;
n=n/10;
if(a[i]>7)
t=1;
i++;
}
i--;
if(t==0)
for(;i>=0;i--)
{
switch(a[i])
{
case 0:
if (!str.empty())
str.append("000");
else
str="000";
break;
case 1:
if (!str.empty())
str.append("001");
else
str="001";
break;
case 2:
if (!str.empty())
str.append("010");
else
str="010";
break;
case 3:
if (!str.empty())
str.append("011");
else
str="011";
break;
case 4:
if (!str.empty())
str.append("100");
else
str="100";
break;
case 5:
if (!str.empty())
str.append("101");
else
str="101";
break;
case 6:
if (!str.empty())
str.append("110");
else
str="110";
break;
case 7:
if (!str.empty())
str.append("111");
else
str="111";
break;
}
}
str1="000000";
str2="000";
if (str.length()==3)
{
str1.append(str);
str=str1;
}
if (str.length()==6)
{
str2.append(str);
str=str2;
}
for (size_t i = 0; i < str_array_size; ++i)
{
str_array[i] = str;
}
ptr = new char [str.size()+1];
strcpy (ptr, str.c_str());
//cout<<ptr;
if(t==1)
{
s="no";
// cout<<"Not a Octal number\n";
return s;
}
return str;
}