I want to have an input to a function similar to python so that then I can loop over it in inside the function. But I am not sure how I should define the input.
func(["a","b","c"])
so that it can also be called
func(["a","b","c", "d"])
is there actually such style of input in c ? I'd be glad if someone also suggested a way of looping over it since my c experience is quite basic.
-------edit,
will be glad if this "[]" style of brackets are possible instead of "{}" similar to python and with minimal code.
CodePudding user response:
Yes, you can use std::initializer_list
to do that:
#include <initializer_list>
template<class T>
void func(std::initializer_list<T> il) {
for (auto x : il);
}
int main() {
func({"a","b","c"});
func({"a","b","c", "d"});
}
will be glad if this "[]" style of brackets are possible instead of "{}" similar to python and with minimal code.
Unfortunately, the multidimensional subscript operator only works in C 23, see p2128 for more details.
CodePudding user response:
You can use a std::initilializer_list
:
#include <iostream>
#include <initializer_list>
void foo(std::initializer_list<std::string> l){
for (const auto& s : l) std::cout << s << " ";
}
int main() {
foo({"a","b","c"});
}
I think python does not distinguish between character and string literals, but C does. "a"
is a string literal, while 'a'
is a character literal. If you actually wanted characters you can use a std::initializer_list<char>
. You can also consider to simply pass a std::string
to the function (foo("abc")
).
will be glad if this "[]" style of brackets are possible instead of "{}" similar to python and with minimal code.
Better get used to different languages being different. Trying to make code in one language look like a different language usually does not pay off, because not only in details python and C are very different.
CodePudding user response:
The other answers will work but I think your looking for std::vector
, which is a array that can dynamically grow and shrink. It is basically the c equivalent to a python list (except you can only store on data type in it).
#include <iostream>
#include <vector>
void foo (std::vector<std::string> vec)
{
// normal for loop
for (int i = 0; i < vec.size (); i )
{
std::cout << vec[i] << std::endl; // do something
}
std::cout << "#########" << std::endl;
// range based for loop
for (auto val : vec)
{
std::cout << val << std::endl;
}
std::cout << "#########" << std::endl;
}
int main ()
{
foo ({'a', 'b', 'c'});
foo ({'a', 'b', 'c', 'd'});
}
replace std::string
with the data type that you need.
CodePudding user response:
I would recommend you to use std::initializer_list
for that purpose.
The function may be defined as follows:
void func(std::initializer_list<std::string> il)
{
for(const std::string & s : il)
{
// ...
}
}
And you may use it the following way:
int main()
{
func({"a", "b", "c"});
return 0;
}
will be glad if this "[]" style of brackets are possible instead of "{}" similar to python and with minimal code.
Python and C are not the same languages and symbols, keywords, etc... have their own meaning. In Python, []
means a list, but in C it is the subscript operator (supposed to be called for a given object), which is a completely different thing.