具有更多参数的模板(Template with more parameters)
我正在尝试进行多用途调试功能。 这有效,但现在我想添加两件事:
- 使其具有可变数量的不同参数。
添加变量名称的输出,例如:a:42 b [0]:54
template<bool B> struct Print {}; template<> struct Print<true> { template<class T> static int do_it (T a) { cout << a << endl; } }; template<> struct Print<false> { template<class T> static int do_it (T & a) { for (int _n(((int)((a).size()))-1), i(0); i <= _n; i++) cout << *(next(a.begin(),i)) << endl; cout << endl; } }; template<class T> int debug (T A){ return Print<std::is_scalar<T>::value>::do_it( A ) ; } int main(){ int a = 42; vector <int> b; b.push_back(54); debug(a); // Works debug(b); // Works debug(a,b); //It's what i'm trying to do. return 0; }
PS:c ++ 11
I'm trying to do a multiuse debug function. This works but now i'd like to add 2 things:
- Make it work with a variable number of different parameters.
Add the output of the name of the variable, as example: a: 42 b[0]: 54
template<bool B> struct Print {}; template<> struct Print<true> { template<class T> static int do_it (T a) { cout << a << endl; } }; template<> struct Print<false> { template<class T> static int do_it (T & a) { for (int _n(((int)((a).size()))-1), i(0); i <= _n; i++) cout << *(next(a.begin(),i)) << endl; cout << endl; } }; template<class T> int debug (T A){ return Print<std::is_scalar<T>::value>::do_it( A ) ; } int main(){ int a = 42; vector <int> b; b.push_back(54); debug(a); // Works debug(b); // Works debug(a,b); //It's what i'm trying to do. return 0; }
PS: c++11
原文:https://stackoverflow.com/questions/26589491
最满意答案
只需阅读它就像读取正常变量一样。
$category = $_REQUEST['category'];
与其他变量不同,它会返回PHP Array 。 您可以像普通的PHP数组一样迭代这个变量。
foreach($category as $cat) { echo $cat; }
请确保使用isset()检查请求是否确实具有您正在查找的值。 否则上面的代码可能会抛出未定义的索引错误。
Just read it like you read normal variables.
$category = $_REQUEST['category'];
Unlike other variables it will return you PHP Array. You can iterate over this variable just like normal PHP Array.
foreach($category as $cat) { echo $cat; }
Make sure your check if request actually have the value you're looking for by using isset(). Otherwise above code might throw undefined indexed error.
相关问答
更多-
方括号表示新阵列。 var ar=new Array("a","b"); var ar=["a","b"]; //Equal to the syntax above 在这种情况下,如果使用方括号,则不会有区别,因为如果它是数组,它将转换为字符串,但如果删除括号,则花费的时间会更少,因为它不必构建新数组并将其转换,但它使用简单的字符串。 Square brackets means new Array. var ar=new Array("a","b"); var ar=["a","b"]; //Equal ...
-
HTML表单数组中的方括号。(Square brackets in HTML form arrays. Just conventional or with a meaning?)[2022-04-04]
它们解决了PHP的限制,如果多个具有相同名称的值被提交,例如从一组复选框或多个选择中提交,它不会自动生成数组。 (IIRC它只返回最后一个值。) 就我个人而言,我一直认为这是一个非常低劣的解决方法。 即使经典ASP也可以应付,而不需要客户端添加标记。 通过这种方式,服务器端平台没有业务在客户端强加标记要求。 They address a limitation of PHP, which doesn't generate an array automatically if multiple values wi ... -
PHP如何在名称中读取带有方括号的表单变量(PHP How do I read the form variables with empty square brackets in their name)[2023-10-21]
只需阅读它就像读取正常变量一样。 $category = $_REQUEST['category']; 与其他变量不同,它会返回PHP Array 。 您可以像普通的PHP数组一样迭代这个变量。 foreach($category as $cat) { echo $cat; } 请确保使用isset()检查请求是否确实具有您正在查找的值。 否则上面的代码可能会抛出未定义的索引错误。 Just read it like you read normal variables. $category = ... -
varchar [(n | max)] 如果未在数据定义或变量声明语句中指定n,则默认长度为1。 https://msdn.microsoft.com/en-AU/library/ms176089.aspx varchar [ ( n | max ) ] When n is not specified in a data definition or variable declaration statement, the default length is 1. https://msdn.microsoft. ...
-
方形braquets是为$_GET和$_POST方法使用的数据定义数组的常规方法。 在PHP脚本中,您需要反序列化数组并解析或执行数组所需的操作。 $position) { //Do what you wan ...
-
用引号括起来 'attendedstudents[]' : Enclose it in Quotes 'attendedstudents[]' :
-
你可以用括号检查参数 String[] name = null; if (request.getParameterValues("name") != null){ name = request.getParameterValues("name"); }else if(request.getParameterValues("name[]") != null){ name = request.getParameterValues("name[]"); } You could check for p ...
-
您需要使用\\来跳过jQuery选择器中的元字符(例如!"#$%&'()*+,./:;<=>?@[\]^{|}~ )。 $('#services\\[3490\\]\\[selected\\]') 从 jQuery选择器文档: 要使用任何元字符(如!"#$%&'()*+,./:;<=>?@[\]^ {|}〜`)作为名称的文字部分,它必须用两个反斜杠转义:例如,一个id =“foo.bar”的元素可以使用选择符$(“#foo \ .bar”)。W3C CSS规范包含一组完整的规则关于有效的CSS选择器 ,M ...
-
PHP不会将表单输入与名称中的方括号转换为数组(PHP not converting form inputs with square brackets in names to arrays)[2023-05-18]
PHP使用parse_str()函数解析它, 该函数仅使用1个现在不推荐使用的设置 因此请检查本地和远程的magic_quotes_gpc设置并进行比较。 仔细查看parse_str()如何解析示例字符串。 你可以得到你的$_SERVER['QUERY_STRING']并在这个字符串上使用parse_str()并查看结果。 另请检查: http://php.net/manual/en/info.configuration.php#ini.magic-quotes-gpc http://php.net/man ... -
如果你使用$something = $people['a value']意味着你要赋值$people数组,其索引a value 所以你没有那个,所以它会抛出未定义的索引错误 。 你正在使用的是一个嵌套的关联数组,你必须使用类似的东西输出 echo $people['Jack']['Age']; 如你想要一个简短的例子,假设你有一个类似的数组 $people = array('name'=>'Jack'); 现在,当您想要将名称存储在变量中时,您可以使用 $store_name = $people['na ...