标签: std

在std :: vector成员中取消分配数组

这是关于std :: vector成员结构中的数组范围的问题.

假设我有下一个代码:

struct memberStruct {
...
char array[5];
...
};

std::vector <memberStruct> _workVector;
Run Code Online (Sandbox Code Playgroud)

问题:哪个向量清除方法(clear/erase/pop_back)确保memberStruct.array取消分配AKA超出范围?

c++ std

0
推荐指数
1
解决办法
67
查看次数

通过std :: ifstream从字符串中获取每个值

我尝试使用ifstreamwhile循环来获取每个值.但是,当我尝试它时,没有任何反应.为什么?

std::string line;
std::getline(cin, line);
std::ifstream stream(line);
while(stream){
    std::cout << stream.get();
}
Run Code Online (Sandbox Code Playgroud)

c++ get std ifstream

0
推荐指数
1
解决办法
84
查看次数

C++ ternary operator with function calls

I'm trying to clean up some code with ternary operators and I'm running into some compiler errors i can't make sense of.

The code I had before looks like this and runs fine.

if(!inFile.good())
     throw -2;
getline(inFile, inLine);
Run Code Online (Sandbox Code Playgroud)

And I'm trying to clean it up using this code instead.

(inFile.good()) ? getline(inFile, inLine) : throw -2;
Run Code Online (Sandbox Code Playgroud)

But I'm getting the following errors.

g++ -w -o test orange_test.cpp
In file included from orange_test.cpp:4:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iostream:39:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/streambuf:558:31: error: base …
Run Code Online (Sandbox Code Playgroud)

c++ std ternary-operator

0
推荐指数
1
解决办法
3179
查看次数

我在c ++中不包含字符串头文件时出错

#include<iostream>
#include<string>

using namespace std;
void main(){
string str="abc";
cout<<str;
system("pause");
}
Run Code Online (Sandbox Code Playgroud)

如果我不包含字符串头文件,那么<< in line cout <中 会出现错误

我认为错误将在定义str的行.

c++ string std

0
推荐指数
1
解决办法
3955
查看次数

C++当类型T需要构造函数时,是否可以创建类型为T的std :: list?

例如:

class apple
{
public:
    string name;

    apple::apple(string name) : name(name)
    {
    }
};
Run Code Online (Sandbox Code Playgroud)

如果我想制作一堆每个都有苹果类型的列表,我想我可以做类似的事情std::list<apple> empire("empire"), macintosh("macintosh").基本上我想为list<T>我创建列表时声明的类T的构造函数传递参数.对不起,如果我没有解释这个问题,如果你有这种能力,请随时编辑我的问题.

谢谢

编辑这个问题似乎令人困惑,这可能是因为我给出了一个糟糕的例子.我需要重新设计我的课程.按照这个例子,虽然我想要的是一个列表,它都是帝国苹果,该列表中的每个苹果都有一个指定类型的帝国,一个列表都是macintosh苹果,该列表中的每个苹果都有一个指定类型的macintosh.

因此,为了澄清一些或在此处混淆一些我们去.

class apple
{
public:
    string variety_name;
    string description;
    apple::apple(string variety_name, string description)
        : variety_name(variety_name), description(description)
    {
    }
};
int _tmain(int argc, _TCHAR* argv[])
{
    // Vlad from Moscow's answer
    std::list<apple> empire(1, apple("empire", "picked yesterday")),
        macintosh(1, apple( "macintosh", "picked yesterday")); 

    // Vaughn Cato's answer
    empire.push_back(apple("empire", "picked today"));
    macintosh.push_back(apple("macintosh", "picked today"));

    for(list<apple>::iterator it=empire.begin(); it != empire.end(); ++it) …
Run Code Online (Sandbox Code Playgroud)

c++ stl std stdlist

0
推荐指数
1
解决办法
493
查看次数

什么时候应该返回std :: ostream?

我回到std::ostream每一个我要创建这样一个操作者的时间std::string操作,显示值(没有操作员),但我不知道为什么.如果它std::ofstream被用作函数成员操作符函数(std::cout),我该如何返回它,何时应该这样做以及为什么?

例:

class MyClass
{
   int val;
   std::ostream& operator<<(const std::ostream& os, const MyClass variable)
  {
      os << variable.val;
  }
}
Run Code Online (Sandbox Code Playgroud)

std::string:

std::string a("This is an example.");
std::cout << a;
Run Code Online (Sandbox Code Playgroud)

c++ std ostream

0
推荐指数
1
解决办法
492
查看次数

在这种情况下我会回报什么?

我将发布我的代码然后解释我的查询:

typedef std::shared_ptr<SEntity> Entity;

//Scene_Ids is an enum
static std::map<Scene_Ids, std::vector<Entity> > m_scene_entities;

std::shared_ptr<SEntity>& SEntityManager::getEntity(const std::string& entity_name)
{
    int counter = 0;

    for (auto iter = m_scene_entities.begin(); iter != m_scene_entities.end(); ++iter)
    {
        if (iter->second[counter]->getId() == entity_name)
            return iter->second[counter];

        counter++;
    }

    //What would I return if the entity couldn't be found?
}
Run Code Online (Sandbox Code Playgroud)

代码基本上解释了这一切.我有一个方法,如果在地图内的std :: vector中找到"实体",它将返回对它的std :: shared_ptr类型的引用.但是,由于我没有返回指针,我无法返回nullptr.在失败的情况下我能回报什么?

另外,我知道std :: shared_ptr意味着在几个不同的地方有副本.为此,我真的需要返回一个引用,还是可以按值返回它?

谢谢!

c++ pointers reference std shared-ptr

0
推荐指数
1
解决办法
73
查看次数

如何将std :: map的前N个元素复制到另一个地图?

我想将std :: map的前N个元素复制到另一个地图.我试过copy_n但是失败了.我怎样才能做到这一点?

#include <iostream>
#include <map>
#include <algorithm>
#include <iterator>
using namespace std;
int main(){
  map<int,int> Map;
  for ( int i=0;i<10;i++) Map[i]=i*i;
  map<int,int> Map2;
  std::copy_n(Map.begin(), 5,  Map2.end());
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ stl std map c++11

0
推荐指数
1
解决办法
930
查看次数

C++ regex_replace问题

为什么这段代码不能将'Mozilla/5.0'替换为'我的用户代理'?

Regex在Notepad ++中运行良好,但在我的控制台应用程序中不起作用.

#include <regex>
#include <string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    string header = 
"GET / HTTP/1.1\r\n\
Host: com.com\r\n\
User-Agent: Mozilla/5.0\r\n\
Connection: keep-alive";

    tr1::regex rx("^(User-Agent:).+$", regex_constants::icase);
    header = regex_replace(header, rx, "$1 My User Agent", regex_constants::format_first_only);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ regex std

0
推荐指数
1
解决办法
107
查看次数

字符串未在此范围C++中声明

我一直在测试一些gdi函数,并想用字符串测试一些东西.但是由于某种原因,当我运行此程序时,我收到以下错误:'string'未在此范围内声明

我甚至试图在开头包含"using namespace std"(通常我不必为创建字符串而做)但在这种情况下,在声明std :: string myString之后; 我得到另一个错误说:错误:'string'不是'std'的成员

我能做错什么?完整的代码在这里:

#define _WIN32_WINNT    0x0500  

#include <tchar.h>
#include <windows.h>


LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");

int WINAPI WinMain (HINSTANCE hThisInstance,
                 HINSTANCE hPrevInstance,
                 LPSTR lpszArgument,
                 int nCmdShow)
{
HWND hwnd;               /* This is the handle for our window */
MSG messages;            /* Here messages to the application are saved */
WNDCLASSEX wincl;        /* Data structure for the windowclass */

/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName …
Run Code Online (Sandbox Code Playgroud)

c++ windows string std

0
推荐指数
1
解决办法
3万
查看次数

标签 统计

c++ ×10

std ×10

stl ×2

string ×2

c++11 ×1

get ×1

ifstream ×1

map ×1

ostream ×1

pointers ×1

reference ×1

regex ×1

shared-ptr ×1

stdlist ×1

ternary-operator ×1

windows ×1