Operator in quantum mechanics I'm really confused by the definition and uses of operators in quantum mechanics. Usually we say that the state of a system is described by some vector $\lvert\psi\rangle$ in a Hilbert space $H$, and then we define operators acting on said vector, for example $\hat{p}: H\rightarrow H$.
But often I read things like $$ \hat{p}\psi(x)=-i\hbar\frac{\partial}{\partial x}\psi(x)$$ 
I don't understand. $\psi(x)=\langle x\rvert\psi\rangle$ is a function in $L^2$ or some other space, not the same Hilbert space as $\lvert\psi\rangle$. More precisely $\psi(x)=\langle x\rvert\psi\rangle$ is an element of the field associated with $H$ for fixed $x$, I don't understand how can we apply $\hat{p}$ to this object. 
How should I interpret this?
EDIT: I just realized that my question is a duplicate of this one, I must say that the "related" section is a much better search engine than the search engine. I have a question about ACuriousMind's answer. He writes that one can define a map $$\mathrm{Ket}: L^2(\mathbb{R},\mathbb{C})\rightarrow \mathcal{H}_{1D}, \psi \mapsto|\psi\rangle := \int_{-\infty}^\infty\psi(x)|x\rangle\mathrm{d}x $$
But I don't really understand how $$\int_{-\infty}^\infty\psi(x)|x\rangle\mathrm{d}x $$ is defined. How can one take an integral of a ket? The integral is a functional in $L^2$, not whatever space $\lvert x \rangle$ is in.
 A: If we you want to know a rigorous formulation of quantum mechanics, please check the first chapter of the book Dirac Kets, Gamow Vectors and Gelfand Tripletes--The Rigged Hilbert Space formulation of Quantum Mechanics by A.Bohm and M.Gadella. This is a huge topic and cannot be answered in a few lines. I list some important facts below.
Complete system of commuting operators
$\{A_k\}$, $k=1,2,\cdots,N$ is a system of commuting operators on rigged Hilbert space $\Phi \subset H \subset \Phi^X$ iff


*

*$[A_i,A_k] = 0$ for all $i,k = 1,\cdots,N$

*$\sum A_k^2$ is essentially self adjoint


$\{A_k\}$ is a complete commuting system if there exists a vector $\phi \in \Phi$ such that $\{A\phi| A$ runs out the algebra generated by $\{A_k\}\}$ spans $H$.
An antilinear functional $F$ on $\Phi$ is a generalized eigenvector for the system $A_k$ if for any $k=1,\cdots,N$
$$(A_k)^X F = \lambda^{(k)}F$$
The set of numbers $\lambda = (\lambda^{(1)},\cdots,\lambda^{(N)})$ are called generalized eigenvalues $F_{\lambda} = |\lambda^{(1)},\cdots,\lambda^{(N)}\rangle$.
Nuclear Spectral Theorem
Let $\{A_k\}$, $k=1,2,\cdots,N$ be a complete system of commuting essentially $\tau_{\Phi}$-continuous operators on the rigged Hilbert space $\Phi \subset H \subset \Phi^X$. Then, there exists a set of generalized eigenvectors
$$|\lambda^{(1)},\cdots,\lambda^{(N)}\rangle \in \Phi^X$$
$$(A_k)^X|\lambda^{(1)},\cdots,\lambda^{(N)}\rangle = \lambda^{(k)}|\lambda^{(1)},\cdots,\lambda^{(N)}\rangle$$
$$\lambda^{(k)} \in \Lambda^{(k)} = \mbox{ spectrum of } A_k$$
such that for every $\phi \in \Phi$ and some uniquely defined measure $\mu$ on $\Lambda = \Lambda^{(1)} \times \cdots \times \Lambda^{(N)}$,
$$(\psi|\phi) = \int_{\Lambda} d\mu(\lambda) \langle \psi | \lambda^{(1)},\cdots,\lambda^{(N)} \rangle \langle \lambda^{(1)},\cdots,\lambda^{(N)} | \phi \rangle$$.
Comments
Roughly speaking, the equivalence of the $L^2(\mathbb{R},\mathbb{C})$ and $H$ is guaranteed by the fact that $X$ is a system of commuting operators on rigged Hilbert space. The demanded rigged Hilbert space should be constructed from the original Hilbert space if the algebra of operators are given. The notation of $|\psi\rangle = \int dx \langle x | \psi \rangle |x\rangle$ holds in the sense of performing inner product and is guaranteed by the nuclear spectrum theorem.
The whole construction is very complicated and subtle, and needs a lot of concepts of modern function analysis. Again, please check the book I recommended if you are really interested in this topic. 
A: The wave function $ \left| \psi\right>$ is an element in some abstract Hilbert Space $H$. Now when we do experiments, we have a to choose a basis. If you are care about position for your experiment then you have to write the wave function in the position eigenbasis. Borrowing the intuition and notation from finite dimensional vector spaces (which I agree is always questionable) we pick the position basis $ \left|x\right>$ and project onto our basis to get $ \left<x \vert \psi \right> := \psi(x) $ .
Now in the abstract Hilbert Space we can also have operators like the momentum operator $ \hat{p}$. We need to figure out how it acts on our wave function. It turns out it is defined as a generator of space translations. To see this, consider the translation operator defined by the action: $$ T_a \left|x\right> = \left|x+a\right>$$
This in means  $\left< \psi\right|T_a\left|x\right> =\left< \psi\vert x+a\right>= \left< x -a \vert \psi \right> = \psi(x-a) $ I used the fact that $ \left<x\right|T^{\dagger}_a = \left<x-a\right| $.  Now we define a connection on our space i.e something that takes us from one point to another thusly, $$ \hat{p} \left| \psi \right> = i \hbar \lim_{a \rightarrow 0}\frac{T_a\left|x\right>  -\left|x\right> }{a} \implies i \hbar\lim_{a \rightarrow 0}\frac{\psi(x-a) - \psi(x)}{a} = i \hbar \frac{d}{dx}\psi(x) $$
How then do we interpret the integral $ \int_{-\infty}^{\infty}\psi(x) \left| x \right >\, dx?  $ Again, using the analogy from finite dimensional spaces we can the think of it as an infinite dimensional analogue of the following
$$ \sum_{n=-M}^{n=M}\psi(a + n \Delta x)\left| a + n \Delta x\right > \Delta x  $$ 
In finite dimensions one can clearly see that we are getting a column vector and the value of a slot in the vector is given by the sum.
In summary, we are just picking a basis in which to write our wave function. If I need the momentum basis then I have to do the projection $ \left< p \vert \psi \right > : = \psi(p) $ . I feel your aganst since we are dealing with infinite dimensional spaces and it is not clear that we rigorously  have an eigenbasis, but it sort of  works so forgive and forget.
