在尝试过滤MainMenu
数组时
const byte menuLength = 10;
struct Menu {
int id;
char Description[16];
int parentId;
};
Menu MainMenu[menuLength] = {
{ 1, "SYSTEM SETUP ", -1 },
{ 2, "FUNCTIONS SETUP", -1 },
{ 3, "FIRMWARE VER. ", -1 },
//SYSTEM SETUP
{ 4, "< BACK ", 1 },
{ 5, "MODEL SELECT ", 1 },
{ 6, "RX SETUP ", 1 },
//FUNCTIONS SETUP
{ 7, "< BACK ", 2 },
{ 8, "REVERSE ", 2 },
{ 9, "ENDPOINTS ", 2 },
};
与此一起
Menu GetSub(int parentId)
{
int position = 0;
Menu Filtered[menuLength];
for (int i = 0; i < menuLength; i++)
{
if (parentId == MainMenu[i].parentId)
{
Filtered[position] = MainMenu[i];
position++;
}
}
return Filtered;
}
我遇到以下错误
那么,我应该如何返回过滤后的数组?
答案 0 :(得分:3)
首先,请在使用from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
def print_to_file(*args):
with open('text.txt', 'a') as fh:
fh.write(' '.join(map(str,args)))
print = print_to_file
class TestApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
Layout = "{!s:1} {!s:2} {!s:3} {!s:4} {!s:5} {!s:6} {!s:7} {!s:8} {!s:8}"
print(Layout.format("Ticker;", "Date;", "None;", "Time;", "Open;", "High;", "Low;", "Close;", "Volume "))
def historicalData(self, reqId, bar):
print("AAPL", ";", bar.date.replace(' ', '; '), ";", bar.open, ";", bar.high, ";", bar.low, ";", bar.close, ";", bar.volume)
def main():
app = TestApp()
app.connect("127.0.0.1", 7497, 0)
contract = Contract ()
contract.symbol = "AAPL"
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
contract.primaryExchange = "NASDAQ"
app.reqHistoricalData(0, contract, "", "1 D", "1 min", "TRADES", 0, 1, False, [])
app.run()
if __name__ == "__main__":
main()
时使用C++
容器。不要使用可变大小的数组(VLA),有lots of条文章介绍了为什么使用它不好。改用std::vector
和std::string
。
C++
您可以使用std::copy_if
过滤掉所需的菜单。
const byte menuLength = 10;
struct Menu {
int id;
std::string Description;
int parentId;
};
std::vector<Menu> MainMenu = {
{ 1, "SYSTEM SETUP ", -1 },
{ 2, "FUNCTIONS SETUP", -1 },
{ 3, "FIRMWARE VER. ", -1 },
//SYSTEM SETUP
{ 4, "< BACK ", 1 },
{ 5, "MODEL SELECT ", 1 },
{ 6, "RX SETUP ", 1 },
//FUNCTIONS SETUP
{ 7, "< BACK ", 2 },
{ 8, "REVERSE ", 2 },
{ 9, "ENDPOINTS ", 2 },
};
有了Eric Niebler的range-v3
库,这变得更加微不足道了。
std::vector<Menu> GetSub(const std::vector<Menu>& menu, int parentId)
{
std::vector<Menu> sub;
std::copy_if(menu.begin(), menu.end(), std::back_inserter(sub), [parentId](const Menu& m) {
return m.parentId == parentId;
});
return sub;
}
答案 1 :(得分:-3)
您试图通过简单的Menu对象返回一个Menu对象数组,您需要更改函数的原型:
Menu GetSub(int parentId);
进入
Menu * GetSub(itn parentId);
此函数将为您返回指向Menu的数组的指针,但随后会出现另一个问题:您实际上并不知道数组中存储了多少个对象。可以通过在代码中添加简单的结构来解决:
struct MenuArray {
Menu * ptr;
int size;
}
然后像这样重新制作函数:
MenuArray GetSub(int parentId)
{
int position = 0;
Menu * Filtered = new Menu[menuLength];
for (int i = 0; i < menuLength; i++)
{
if (parentId == MainMenu[i].parentId)
{
Filtered[position] = MainMenu[i];
position++;
}
}
return MenuArray{Filtered, position};
}
我们还可以简单地使用STL向量,这是您可以使用的最简单的解决方案。
using std::vector;
vector<Menu> GetSub(int parentId)
{
vector<Menu> Filtered(menuLength);
for (int i = 0; i < menuLength; i++)
if (parentId == MainMenu[i].parentId)
{
Filtered.push_back(MainMenu[i]);
}
return Filtered;
}