5
That's right - you can't have null values in the list unless you actually add them :)
Accepted 6
To convert float (or any other numeric type) to a string, just call its ToString() method with or without a formatting argument. For example:
float f = 2.3f;
String ^ fStr = f.ToString("f3");
Console::WriteLine(fStr); // 2.300
Here's some basic code for finding the maximum float in a collection of floats:
array<float>^ floats = {1.0f, 2.45f, -2.3f, 5.21f, 0.04f, 1.7f};
float max = Single::MinValue; // 'impossible' value
for each(float f in floats)
{
if (f > max) max = f;
}
Console::WriteLine(max); // 5.21
6
Sure I can:
int i = 17; // or whatever
String ^binFromInt = Convert::ToString(i, 2);
Console::WriteLine(binFromInt); // 10001
String ^hex = "1a"; // or whatever
int j = Convert::ToInt32(hex, 16);
String ^binFromHex = Convert::ToString(j, 2);
Console::WriteLine(binFromHex); // 11010
String ^bin = "10001";
int k = Convert::ToInt32(bin, 2);
Console::WriteLine(k); // 17
String ^bin2 = "11010";
int l = Convert::ToInt32(bin2, 2);
String ^hexFromBin = Convert::ToString(l, 16);
Console::WriteLine(hexFromBin); // 1a
5
Just going back to your post where you had a line marked in red, it may be that what you're looking for here is a static constructor. This is called (at most) once in a program when you first access a static member or create an instance of the class. It is called automatically by the system and you don't have to do anything manually:
ref class classname
{
public:
static array<string ^> ^language_string_id;
classname(void)
{
}
~classname(void)
{
}
private:
// static constructor
static MyClass()
{
language_string_id = gcnew array<String ^>(0);
}
};
The array referred to by language_string_id won't now be affected by the creation of instances.
There is no such thing as a static destructor but, if you wanted to get rid of the array at any time, you could simply do:
namespace::classname::language_string_id = nullptr;
and the garbage collector will, in due course, destroy it for you.
5
You can use the PadLeft method to pad out the number with leading zeros to the required length:
String ^ binary = "100";
String ^ paddedBinary = binary->PadLeft(5, '0');
Console::WriteLine(paddedBinary); // 00100
5
This should work:
#include "stdafx.h"
using namespace System;
using namespace System::Collections::Generic;
int main(array<System::String ^> ^args)
{
array<String ^> ^arr =
{
"1234",
"2345",
"2343",
"4356",
"6434",
"2343",
"2222",
"4545",
"6666",
"6653"
};
List<String ^> ^list = gcnew List<String ^>(arr);
array<int> ^results = gcnew array<int>(10);
for each(String ^s in list)
{
int i = s[0] - 48;
results[i]++;
}
for(int i = 1; i < 10; i++)
{
results[i] = 10 * results[i] + i; // EDITED
}
Array::Sort(results);
Array::Reverse(results);
Console::WriteLine("Top 3 occurrences are\n");
for(int i = 0; i < 3; i++) Console::WriteLine("{0} occurs {1} time(s)", results[i] % 10, results[i] / 10); // EDITED
Console::ReadKey();
return 0;
}
0
Ya ya its good if we use list right. just we can add list->add and can also restrict this undefined values right? Then i should do that i hope.
0
Instead of 'null' you should use 'nullptr' in C++/CLI.
Incidentally, why are you resizing arrays when the List<string> class does all that automatically for you in the background?
0
Ok Vulpes, probably i need to re look at that. Ok that's fine.
What about my other question?
By default all the array elements will be "undefined values" if i just create it as follows:
String ^ copy = "";
array <String ^> ^ lang;
for(int i = 0; i < 6; i++)
{
Array::Resize(lang, lang->Length + 1);
lang[lang - 1] = lang[lang - 1] + copy;
}
Due to some operation i am missing the last element of the array, which shows up as undefined value. Is there a way how i can validate this i.e i want to be sure that all the elements of the array are valid before doing some operation on the array.
I want do as
if(lang[lang - 1] != null) //null how we do it in "C"
{
//my operation on "lang" array
}
0
No Vulpes, the example which you stated doesn't suit my application as the instance which i want to create is from different class and namespace.
0
OK, here's a simple example of what I mean:
// myclass.h
#pragma once
ref class MyClass
{
public:
array<int>^ MyArray; // instance member variable
MyClass(void)
{
MyArray = gcnew array<int>(4);
}
~MyClass(void)
{
}
};
// mainproj.cpp : main project file.
#include "stdafx.h"
#include "MyClass.h"
using namespace System;
int main(array<System::String ^> ^args)
{
MyClass ^mc = gcnew MyClass();
for(int i = 0; i < 4; i++) mc->MyArray[i] = i;
MyClass ^mc2 = gcnew MyClass(); // create a second instance
for(int i = 0; i < 4; i++) mc2->MyArray[i] = i * 2;
Console::WriteLine(mc->MyArray[1]); // 1 as unaffected by creation of second instance
Console::WriteLine(mc2->MyArray[1]); // 2
Console::ReadKey();
return 0;
}
0
Can you please quote an example what you stated below?
0
If language_string_id is a static member variable of classname, then its value will be overwritten each time you create a new instance of the class because there's only one copy of a static variable.
If you don't want this to happen, then you need to make language_string_id into an instance member variable instead. There will be then be a separate copy for each instance you create.
0
Vulpes probably you didn't get what i said.
If i write the line
instancename = new classname();
classname(void)
{
language_string_id = gcnew array<String ^>(0);
}
~classname(void)
{
delete language_string_id;
}
The text marked in red contains data which i load when i call it with 1st instance, will be lost if i create instance as you stated. So i tried writing as below:
classname ^ instancename2;
which is showing up a warning c4101 : unreferenced local variable. Can i use it that way or is there any other way to do?
0
If you're creating a new instance but you still want to be able to reference the existing instance, then you should use a different variable:
classname ^ instancename2 = new classname();
However, if you're not bothered about referencing the existing instance again, then you may be able to reuse the existing variable:
instancename = new classname();
If there are no longer any variables pointing to the existing instance, then it will be destroyed by the garbage collector in due course. You only need to dispose of it manually, if it is holding references to unmanaged resources (file handles, database connections etc) which need to be released as soon as possible.
0
for the 1st question which i asked how to find invalid (or undefined) value.
and for the second question i am asking how to declare an instance if i should not populate the array in the class again. If i write a line like this
static namespace::classname ^ instancename;
instancename= gcnew namespace::classname ();
again when i want to create instance of the same class for second time the array gets loaded again so i declared as follows,
LanguageTable ^ PrintLanguageData;
will this work or else should i change that?
0
When you use the Array::Resize() method, a new array object is created in the background with the required size, the elements of the old array are copied across to the new one and then the new array is assigned to the 'by reference' parameter.
This means that the extra elements will, initially, all have their default value which will be zero in the case of numbers or nullptr in the case of strings.
The best thing to do is to assign valid values to the extra elements as soon as you've resized the Array but, if for some reason you can't do that, then you just have to put in a check for invalid values and take the appropriate action if one is discovered.
Sorry, I didn't really follow your second question. Are you saying that you want to reuse the array you've already created in another place or are you wanting to make sure that the first array is disposed of before you create a new one and assign it to the same variable?
0
How to find if something is defined or not?
I am incrementing my array size
Array::Resize(lang, lang->Length + 1);
after that sometimes one of the array element is not defined and hence causing break to the normal execution. how to validate whether anything is validated or not?
And more thing is i have created an instance to a class as follows:
static namespace::classname ^ instancename;
instancename= gcnew namespace::classname ();
Now i want create instance in some other place same as above but if i do that a array which is written in above class is getting loaded again which causing the length and elements to get loaded again. so how can i avoid this.
I wrote like this which is working fine but showing a warning. will this work perfectly or should i change that.?
classname ^ instancename2;
0
Sorry Vulpes i posted and immediately after that i found the answer and hence i deleted the post. I did it through System::Convert::ToDouble(string_name);
I think Single::Parse(fStr); will generate and exception if it couldn't be parsed right? anyways the method i stated is good i think. anyways thanks vulpes.
0
Your last post is not showing up (even though e-mail notification received) but, if you're sure the conversion will succeed, then to convert from string to float, use:
String ^fStr = "2.34";
float f = Single::Parse(fStr);
Console::WriteLine(f);
or, if you're not sure it'll succeed:
String ^fStr = "2.34";
float f;
bool isValid = Single::TryParse(fStr, f);
if (isValid) Console::WriteLine(f);
0
if the binary value comes as "100" after conversion where as i need the binary value to look like "00100" what can i do?
its like i need binary value to occupy 5 places even though it is lees than that. can you please tell me how i can make that happen?
0
I must say i have become a fan of you in the recent times. If you give this kind of support i will do my job perfectly i hope.
0
One more small doubt. Can you tell me how to convert a decimal and hexadecimal to binary and vice versa.
0
Done got it. Once again thanks for your answer and nice explanation as well. Go and enjoy your supper.
0
The digits '0' to '9' have unicode values of 48 to 57. So, after converting the first character to an integer, we need to subtract 48 to get the actual digit.
Using a multiplier (such as 100000) on the number of occurrences and then adding the digit, is a way of sorting the occurrences but preserving the digit. So, even after they have been sorted, we can still recover both the digit and the number of times it occurs.
However, my answer was a bit rushed (it was nearly time for lunch!) and a multiplier of 10 rather than 100000 would actually suffice here (I've edited my previous post).
0
int i = s[0] - 48;
what does that 48 mean?
results[i] = 100000 * results[i] + i;
what does this 100000 mean and why are you multiplying it with results[i] and adding with i?