C++: wchar_t cannot be stored in a std::map as a key or value - c++

I am trying to make a variable with the data type std::map<char, wchar_t> in C++. When I try this, Visual Studio gives this build error:
C2440 'initializing': cannot convert from 'initializer list' to 'std::map<char,wchar_t,std::less<char>,std::allocator<std::pair<const char,wchar_t>>>'
The same error also occurs when wchar_t is the key data type and char is the value data type.
You can replicate this error by running the following code:
const std::map<char, wchar_t> UNICODE_MAP = {
{ 'X', L"█" },
{ 'G', L"▓" },
{ 'O', L"ᗣ" },
{ 'P', L"ᗤ" }
};
How would I make a map with the key as a char and the value as a wchar_t data type?

It's a map from narrow char to wide char, not char to wide string:
Instead of this:
const std::map<char, wchar_t> UNICODE_MAP = {
{ 'X', L"█" },
{ 'G', L"▓" },
{ 'O', L"ᗣ" },
{ 'P', L"ᗤ" }
};
Use this:
const std::map<char, wchar_t> UNICODE_MAP = {
{ 'X', L'█' },
{ 'G', L'▓' },
{ 'O', L'ᗣ' },
{ 'P', L'ᗤ' }
};
If you actually need the value to be a string, then declare it as:
const std::map<char, std::wstring> UNICODE_MAP = {
{ 'X', L"█" },
...
Also, while I'm here, I advocate for escaping all Unicode chars in source files that are not in the ASCII range. Between IDEs with varying behaviors, revision control systems, and the guy down the hall who didn't read your memo about "save as Unicode", this can get easily corrupted. So... my final recommendation is this:
const std::map<char, wchar_t> UNICODE_MAP = {
{ 'X', L'\u2588' },
{ 'G', L'\u2593' },
{ 'O', L'\u15e3' },
{ 'P', L'\u15e4' }
};

Related

How can I i get the same result as shown on the docstring

def make_str_from_row(board, row_index):
""" (list of list of str, int) -> str
Return the characters from the row of the board with index row_index
as a single string.
>>> make_str_from_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 0)
'ANTT'
>>> make_str_from_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 1)
'XSOB'
>>> make_str_from_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B'], ['F', 'G', 'O', 'L']], 2)
'FGOL'
"""
Use str.join:
def make_str_from_row(board, row_index):
"""(list of list of str, int) -> str
Return the characters from the row of the board with index row_index
as a single string.
>>> make_str_from_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 0)
'ANTT'
>>> make_str_from_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 1)
'XSOB'
>>> make_str_from_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B'], ['F', 'G', 'O', 'L']], 2)
'FGOL'
"""
return "".join(board[row_index])
print(make_str_from_row([["A", "N", "T", "T"], ["X", "S", "O", "B"]], 0))
print(make_str_from_row([["A", "N", "T", "T"], ["X", "S", "O", "B"]], 1))
print(
make_str_from_row(
[["A", "N", "T", "T"], ["X", "S", "O", "B"], ["F", "G", "O", "L"]], 2
)
)
Prints:
ANTT
XSOB
FGOL

Why the error only be reported on the second element and how can I solve it?

I try to build a char array.
#include <iostream>
#include <vector>
using namespace std;
int main(){
static const char array1[] = {"green", "red","red", "green", "green"};
}
I would expect this to work, but I get the following error:
You are trying to initialize the char array with multiple string literals which decay to const char*. So either use characters in your list initialization:
const char array1[] = { 'g', 'r', 'r', 'g', 'g' };
or change the array signature to const char*:
const char* array1[] = { "green", "red", "red", "green", "green" };
or simply use a std::string type:
std::string array1[] = { "green", "red","red", "green", "green" };
or even better, a vector of strings:
std::vector<std::string> v = { "green", "red", "red", "green", "green" };
This is not an array of chars. Its an Array of strings.
You could write
static const char* array1[] = {"green", "red","red", "green", "green"};
or
static const std::string array1[] = {"green", "red","red", "green", "green"};
Also the error is on the second entry because "green" is an array of chars.

Find element in a nested list and transform (groovy)

If I have a Groovy nested list such as:
List list = ['a', 'b', 'c', ['d', 'e', ['f']]]
I would like to be able to search the list for a particular element, say 'd' and then transform that element to something else such as ['g', 'h'] so that the new list looks like:
['a', 'b', 'c', [['g', 'h'], 'e', ['f']]]
Like this??
List list = ['a', 'b', 'c', ['d', 'e', ['f']]]
assert list.collectNested{
if (it == 'd') {
['g', 'h']
} else {
it
}
} == ['a', 'b', 'c', [['g', 'h'], 'e', ['f']]]
Use following generic approach :-
def findAndReplace(def listValue, def listIndex, def valueToCompare, def valueToReplace, def originalList) {
if(listValue instanceof List) {
listValue.eachWithIndex { insideListValue, insideListIndex ->
findAndReplace(insideListValue, insideListIndex, valueToCompare, valueToReplace, listValue)
}
}else if(listValue == valueToCompare) {
originalList[listIndex] = valueToReplace
}
}
List originalList = ['a', 'b', 'c', ['d', 'e', ['f']]]
def valueToCompare = 'd'
def valueToReplace = ['g', 'h']
originalList.eachWithIndex { listValue, listIndex ->
findAndReplace(listValue, listIndex, valueToCompare, valueToReplace, originalList)
}
println originalList
Output: [a, b, c, [[g, h], e, [f]]]
Hope it will help you...:)

Using json-spirit to read json string in C++

How to use json-spirit to read json string in C++? I read the demo code.
I find that:
const Address addrs[5] = { { 42, "East Street", "Newtown", "Essex", "England" },
{ 1, "West Street", "Hull", "Yorkshire", "England" },
{ 12, "South Road", "Aberystwyth", "Dyfed", "Wales" },
{ 45, "North Road", "Paignton", "Devon", "England" },
{ 78, "Upper Street", "Ware", "Hertfordshire", "England" } };
Can I convert a String into a json object?
char* jsonStr = "{'name', 'Tom'}";
json_spirit provides bool read_string( const String_type& s, Value_type& value ) and bool read( const std::string& s, Value& value ) to read json data from strings.
Here is an example:
string name;
string jsonStr("{\"name\":\"Tom\"}");
json_spirit::Value val;
auto success = json_spirit::read_string(jsonStr, val);
if (success) {
auto jsonObject = val.get_obj();
for (auto entry : jsonObject) {
if (entry.name_ == "name" && entry.value_.type() == json_spirit::Value_type::str_type) {
name = entry.value_.get_str();
break;
}
}
}
You could also use ifstream instead of string to read from json from file.
Please note, according to RFC4627 a string begins and ends with quotation marks.

Invalid cell coordinate phpExcel

I have tested that if using vlookup to search in the self sheet "=vlookup(h2, A2:B2000, 2, False)" and returning value from other sheet "=SupplierList!A2" and "=vlookup(SupplierList!H3, A2:B2000, 2, False)" are also fine.
so why only "=vlookup(h3, SupplierList!A2:B2000, 2, False)" drives into error?
--------------------------- ERROR MESSAGE -------------------------------
Fatal error: Uncaught exception 'Exception' with message 'PO!G2 -> Invalid cell coordinate A' in C:\Program Files\EasyPHP-5.3.6.0\www\ExcelImporter\Classes\PHPExcel\Cell.php:288 Stack trace: #0 C:\Program Files\EasyPHP-5.3.6.0\www\ExcelImporter\Classes\PHPExcel\Cell.php(204): PHPExcel_Cell->getCalculatedValue() #1 C:\Program Files\EasyPHP-5.3.6.0\www\ExcelImporter\Documentation\Examples\index.php(36): PHPExcel_Cell->getFormattedValue() #2 {main} thrown in C:\Program Files\EasyPHP-5.3.6.0\www\ExcelImporter\Classes\PHPExcel\Cell.php on line 288
--------------------------- ERROR MESSAGE -------------------------------
The Cell is with formula like this
**=IF(ISERROR(VLOOKUP(H2349,'Supplier List'!A:B,2,FALSE)),"-",VLOOKUP(H2349,'Supplier List'!A:B,2,FALSE))**
Too long to be a comment...
When debugging calculation errors, I use the following:
function testFormula($sheet,$cell) {
$formulaValue = $sheet->getCell($cell)->getValue();
echo '<b>'.$cell.' Value is </b>'.$formulaValue."<br />\n";
$expectedValue = $sheet->getCell($cell)->getOldCalculatedValue();
echo '<b>'.$cell.' Expected Value is </b>'.((!is_null($expectedValue)) ? $expectedValue : 'UNKNOWN')."<br />\n";
$calculate = false;
try {
$tokens = PHPExcel_Calculation::getInstance()->parseFormula($formulaValue,$sheet->getCell($cell));
echo '<b>Parser Stack :-</b><pre>';
print_r($tokens);
echo '</pre>';
$calculate = true;
} catch (Exception $e) {
echo "PARSER ERROR: ".$e->getMessage()."<br />\n";
echo '<b>Parser Stack :-</b><pre>';
print_r($tokens);
echo '</pre>';
}
if ($calculate) {
try {
$cellValue = $sheet->getCell($cell)->getCalculatedValue();
echo '<b>'.$cell.' Calculated Value is </b>'.$cellValue."<br />\n";
echo '<h3>Evaluation Log:</h3><pre>';
print_r(PHPExcel_Calculation::getInstance()->debugLog);
echo '</pre>';
} catch (Exception $e) {
echo "CALCULATION ENGINE ERROR: ".$e->getMessage()."<br />\n";
echo '<h3>Evaluation Log:</h3><pre>';
print_r(PHPExcel_Calculation::getInstance()->debugLog);
echo '</pre>';
}
}
}
$sheet = $objPHPExcel->getActiveSheet();
PHPExcel_Calculation::getInstance()->writeDebugLog = true;
testFormula($sheet,'A4');
This provides a fairly detailed log of everything that the calculation engine is doing, allowing the cause of the problem to be identified.
If you simply want to disable cell value calculation when saving a file, then you can issue the following command after instantiating yor writer, but before issuing the save command:
$objWriter->setPreCalculateFormulas(FALSE);
para resolver esse problema "Invalid cell coordinate" $this->caracteres = array(
' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'Y', 'W', 'Z',
'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM',
'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AX', 'AY', 'AW', 'AZ');
adicione mais caracteres ,
'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM',
'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AX', 'AY', 'AW', 'AZ'); ....
English
to solve this problem "Invalid cell coordinate" $ this-> characters = array (
             '', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L' , 'M',
             'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'Y', 'W', 'Z '
             'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM '
             'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AX', 'AY', 'AW', 'AZ ');
add more characters,
             'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM '
             'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AX', 'AY', 'AW', 'AZ '); ....